Terellion
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,([s tring])
$colOSType = New-Object system.Data.DataColumn OperatingSystemType,([stri ng])
$tableOS.columns.add($colO S)
$tableOS.columns.add($colO Sversion)
$tableOS.columns.add($colO SType)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows 8.1"
$rowtableOS.OperatingSyste mVersion = "6.3"
$rowtableOS.OperatingSyste mType = "WorkStation"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows 8"
$rowtableOS.OperatingSyste mVersion = "6.2"
$rowtableOS.OperatingSyste mType = "WorkStation"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows 7"
$rowtableOS.OperatingSyste mVersion = "6.1"
$rowtableOS.OperatingSyste mType = "WorkStation"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows Vista"
$rowtableOS.OperatingSyste mType = "WorkStation"
$rowtableOS.OperatingSyste mVersion = "6.0"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows XP"
$rowtableOS.OperatingSyste mVersion = "5.1"
$rowtableOS.OperatingSyste mType = "WorkStation"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows 2000 Professional"
$rowtableOS.OperatingSyste mVersion = "5.0"
$rowtableOS.OperatingSyste mType = "WorkStation"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows Server 2012 R2"
$rowtableOS.OperatingSyste mVersion = "6.3"
$rowtableOS.OperatingSyste mType = "Server"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows Server 2012"
$rowtableOS.OperatingSyste mVersion = "6.2"
$rowtableOS.OperatingSyste mType = "Server"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows Server 2008 R2"
$rowtableOS.OperatingSyste mVersion = "6.1"
$rowtableOS.OperatingSyste mType = "Server"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows Server® 2008"
$rowtableOS.OperatingSyste mVersion = "6.0"
$rowtableOS.OperatingSyste mType = "Server"
$tableOS.Rows.Add($rowtabl eOS)
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste m = "Windows Server 2003"
$rowtableOS.OperatingSyste mVersion = "5.2"
$rowtableOS.OperatingSyste mType = "Server"
$tableOS.Rows.Add($rowtabl eOS)
$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.OperatingSystemVer sion + "*))"
$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.OperatingSystemVer sion + "*))"
$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.OperatingSystemVer sion + "*)))"
}
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,([s
$colOSType = New-Object system.Data.DataColumn OperatingSystemType,([stri
$tableOS.columns.add($colO
$tableOS.columns.add($colO
$tableOS.columns.add($colO
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$rowtableOS = $tableOS.NewRow()
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$rowtableOS.OperatingSyste
$tableOS.Rows.Add($rowtabl
$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
$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
$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
}
Sounds like a fun project... I'll have a look at it...
ASKER
Thanks very much :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is absolutely superb thank you so much guys!
Thanks footech for writing that... Been a busy few days. Nice and neat ;-)