Count Number of Machines by Operating System

Hey guys,

I've been looking online for a script that could do a get-adcomputer search of an AD Environment and return a count of machines by operating system but then to email it. I'd like it to look something like this:

Operating System:                                                              Count:

Windows XP Professional                                                     400
Windows Server 2012                                                            70


Something along those lines.

I've found a script that retrieves the list but just needs to be emailed to me if possible.





cls

$tableOSName = "OperatingSystems"

$tableOS = New-Object system.Data.DataTable “$tableOSName”

$colOS = New-Object system.Data.DataColumn OperatingSystem,([string])

$colOSversion = New-Object system.Data.DataColumn OperatingSystemVersion,([string])

$colOSType = New-Object system.Data.DataColumn OperatingSystemType,([string])

$tableOS.columns.add($colOS)

$tableOS.columns.add($colOSversion)

$tableOS.columns.add($colOSType)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 8.1"

$rowtableOS.OperatingSystemVersion = "6.3"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 8"

$rowtableOS.OperatingSystemVersion = "6.2"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 7"

$rowtableOS.OperatingSystemVersion = "6.1"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Vista"

$rowtableOS.OperatingSystemType = "WorkStation"

$rowtableOS.OperatingSystemVersion = "6.0"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows XP"

$rowtableOS.OperatingSystemVersion = "5.1"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 2000 Professional"

$rowtableOS.OperatingSystemVersion = "5.0"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2012 R2"

$rowtableOS.OperatingSystemVersion = "6.3"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2012"

$rowtableOS.OperatingSystemVersion = "6.2"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2008 R2"

$rowtableOS.OperatingSystemVersion = "6.1"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server® 2008"

$rowtableOS.OperatingSystemVersion = "6.0"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2003"

$rowtableOS.OperatingSystemVersion = "5.2"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

write-host "Workstation Operating Systems in Greggs.Group: " -foregroundcolor "Green"

$WorkStationCount = 0

foreach ($object in ($tableOS | where {$_.OperatingSystemType -eq 'WorkStation'}))

{

      $LDAPFilter = "(&(operatingsystem=" + $object.OperatingSystem + "*)(operatingsystemversion=" + $object.OperatingSystemVersion + "*))"

      $OSCount = (Get-ADComputer -LDAPFilter $LDAPFilter).Count

      if ($OSCount -ne $null)

      {

            "" + $object.OperatingSystem  + ": " + $OSCount + ""

      }

      else

      {

            "" + $object.OperatingSystem  + ": 0"

            $OSCount = 0

      }

      $WorkStationCount += $OSCount

}

$WorkStationTotalNumber = "Total Number : " + $WorkStationCount + ""

write-host $WorkStationTotalNumber -foregroundcolor "Yellow"

write-host ""

write-host "Windows Server Operating Systems in Greggs.Group: " -foregroundcolor "Green"

$ServerCount = 0

foreach ($object in ($tableOS | where {$_.OperatingSystemType -eq 'Server'}))

{

      $LDAPFilter = "(&(operatingsystem=" + $object.OperatingSystem + "*)(operatingsystemversion=" + $object.OperatingSystemVersion + "*))"

      $OSCount = (Get-ADComputer -LDAPFilter $LDAPFilter).Count

      if ($OSCount -ne $null)

      {

            "" + $object.OperatingSystem  + ": " + $OSCount + ""

      }

      else

      {

            "" + $object.OperatingSystem  + ": 0"

            $OSCount = 0

      }

      $ServerCount += $OSCount

}

$ServerTotalNumber = "Total Number : " + $ServerCount + ""

write-host $ServerTotalNumber -foregroundcolor "Yellow"

write-host ""

$LDAPFilter = "(&(operatingsystem=*)"

foreach ($object in $tableOS)

{

      $LDAPFilter += "(!(&(operatingsystem=" + $object.OperatingSystem + "*)(operatingsystemversion=" + $object.OperatingSystemVersion + "*)))"

}
TerellionAsked:
Who is Participating?
 
footechCommented:
I think Rob's absolutely right.
$body = Get-ADComputer -filter * -Properties operatingsystem | group operatingsystem -NoElement | ft -auto | out-string
Send-MailMessage -to "me@domain.com" -From "ps-test@domain.com" -SmtpServer "smtp.domain.com" -Subject "test comp count" -body $body

Open in new window

0
 
Guy LidbetterCommented:
Sounds like a fun project... I'll have a look at it...
0
 
TerellionAuthor Commented:
Thanks very much :)
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
RobSampsonCommented:
That script looks like overkill for what it collects. You should just be able to query AD for the OperatingSystem attribute, then use Format-Table or Format-HTML and Group-Object to give you the counts.

Rob.
0
 
TerellionAuthor Commented:
That is absolutely superb thank you so much guys!
0
 
RobSampsonCommented:
Thanks footech for writing that... Been a busy few days. Nice and neat ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.