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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy LidbetterCommented:
Sounds like a fun project... I'll have a look at it...
0
TerellionAuthor Commented:
Thanks very much :)
0
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
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.