Server list report
Desktop OSes like Windows XP, Vista, 7, 8, 10, Mac OS X have been removed from the list
#AD Server list
#By default the password for the computer object is reset every 30 days
#pwdLastSet attribute is replicated to all Domain Controllers
$StartTime = Get-Date
$ServersFile= "C:\scripts\Output\Servers.csv"
$smtpServer = "smtp"
$To = "Kevin Curran <kcurran@blah.com>" $From = "Kevin Curran <kcurran@blah.com>" Function check-even ($num) {[bool]!($num%2)}
$counter = $null
$computers = Get-ADComputer -filter * -Properties pwdlastset, Description, operatingSystem,
operatingSystemServicePack, pwdLastSet, whenCreated, CanonicalName
$computers = $computers | select Name, OperatingSystem, OperatingSystemServicePack, whenCreated,
@{n="pwdLastSet";e={[Datetime]::FromFileTime($_.pwdLastSet)}},
@{n="OU";e={$_.CanonicalName -Replace '/[^/]*$',''}}
$Servers = $Computers | where {$_.OperatingSystem -notlike "*XP*" -and
$_.OperatingSystem -notlike "Windows 7*" -and
$_.OperatingSystem -notlike "Windows 8*" -and
$_.OperatingSystem -notlike "Windows 10*" -and
$_.OperatingSystem -notlike "Windows Vista*" -and
$_.OperatingSystem -notlike "Mac OS X"}
$TotalServers = $Servers.count
$ServersCountsByOS = $Servers | Group-Object -property OperatingSystem | sort count -desc | select count,name
# foreach ($machine in $Servers)
# {
# $counter ++
# $DNS = $null
# write-host "$counter of $TotalServers testing connection to $($machine.name)"
# $DNS = [System.Net.Dns]::GetHostByName($machine.name)
# If ($DNS) {
# $machine | Add-Member -membertype noteproperty -name IPs -Value `
# $([String]::Join(" ",$($DNS.AddressList.IPAddressToString)))
# $machine | Add-Member -membertype noteproperty -name Pingable -Value $(Test-Connection -Quiet -Count 1 -ComputerName $machine.name)
# #need to catch error "No such host is known"
# }
# }
$Servers | Export-Csv -NoTypeInformation $ServersFile
$StopTime = Get-Date
$ElapsedTime =$StopTime - $StartTime
Write-host "Script completed in $([Math]::Round($ElapsedTime.TotalSeconds,0)) Seconds `r`n"
#need to create this an empty array so it will not be treated as 1 line string an break the ModifiedHTML function
$message = @()
$message = $message + "The attached list of servers was pulled from AD <br>"
$message = $message + "Desktop OSes like Windows XP, Vista, 7, 8, 10, Mac OS X have been removed from the list <br>"
$message += $ServersCountsByOS | ConvertTo-Html -Fragment -PreContent "<H2>Counts of Servers by OS </H2>" | ForEach-Object {$_}
$ModifiedHTML=ForEach ($line in $Message)
{
$counter++
if (check-even $counter)
{$line.replace('<tr><td>','<tr class="d0"><td>')}
Else
{$line.replace('<tr><td>','<tr class="d1"><td>')}
}
$PreMessage = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
<style type="text/css">
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color:MidnightBlue; color:Yellow;}
TD{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
TR.D0 TD {background-color: White; }
TR.D1 TD {background-color: LawnGreen; }
<!-- This nth-child CSS does not seem to be processed by email clients -->
tr:nth-child(odd) {background-color: White;}
tr:nth-child(even) {background-color: LawnGreen;}
</style>
</head><body>
"@
$PostMessage = "<br>This script was run by " + $env:username + " on " + $env:COMPUTERNAME
$PostMessage += "<br>Script completed in $([Math]::Round($ElapsedTime.TotalMinutes,0)) Minutes"
$PostMessage += "<br>ScriptName: $($MyInvocation.MyCommand) "
$PostMessage += "<br>Script path: $(Split-Path -Parent $MyInvocation.MyCommand.Path)"
$PostMessage += "</body></html>"
$html = $PreMessage + $($ModifiedHTML | out-string) + $PostMessage
#Send Email
Send-MailMessage -From $From -To $To -SmtpServer $smtpServer `
-Subject "AD Server list" -BodyAsHtml $html -Attachments $ServersFile
|