Link to home
Start Free TrialLog in
Avatar of Terellion
TerellionFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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 + "*)))"

}
Avatar of Guy Lidbetter
Guy Lidbetter
Flag of United Kingdom of Great Britain and Northern Ireland image

Sounds like a fun project... I'll have a look at it...
Avatar of Terellion

ASKER

Thanks very much :)
SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is absolutely superb thank you so much guys!
Thanks footech for writing that... Been a busy few days. Nice and neat ;-)