Code:
$ErrorActionPreference = "SilentlyContinue"
#Replace with your computer OU
$OU = "OU=Computers,DC=domain,DC=local"
$LdapFilter = "(&(objectCategory=computer))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$OU)
$objADsearcher = New-Object System.DirectoryServices.DirectorySearcher
$objADsearcher.SearchRoot = $objDomain
$objADsearcher.filter = "$LdapFilter"
$colProplist = "cn"
foreach ($i in $colPropList){$objADsearcher.PropertiesToLoad.Add($i)}
$objResults = $objADsearcher.findall()
$AllComputers = @()
$ComputerObj = @()
$ComputerObj = New-Object psObject
Foreach ($objresult in $objResults ) {
$ComputerObj = @()
$ComputerObj = New-Object psObject
$CN = ($objResult.GetDirectoryEntry().cn).ToString().TrimEnd()
$ComputerObj | Add-Member -MemberType NoteProperty -Name "CN" -Value $CN -Force
If ($result = test-connection -computername $CN -Quiet -Count 2 -TimeToLive 5){
$ComputerObj | Add-Member -MemberType NoteProperty -Name "Model" -Value ((Get-WmiObject -class win32_computersystem -computername $CN).model) -Force
$ComputerObj | Add-Member -MemberType NoteProperty -Name "Manufacturer" -Value ((Get-WmiObject -class win32_bios -computername $CN).manufacturer) -Force
$ComputerObj | Add-Member -MemberType NoteProperty -Name "Serial" -Value ((Get-WmiObject -class win32_bios -computername $CN).serialnumber) -Force
} Else {
$ComputerObj | Add-Member -MemberType NoteProperty -Name "Model" -Value "Not Found" -Force
$ComputerObj | Add-Member -MemberType NoteProperty -Name "Manufacturer" -Value "Not Found" -Force
$ComputerObj | Add-Member -MemberType NoteProperty -Name "Serial" -Value "Not Found" -Force
}
$ComputerObj | ft
$AllComputers += $ComputerObj
}
$file = "ComputerInventory_" + (Get-Date -Format "yyyyMMdd") + ".csv"
$AllComputers |Sort-Object "CN" | Export-Csv "$file" -NoTypeInformation Requires Powershell. I'm using 2.0 but I don't think there is anything in there that won't work in 1.0. Haven't tested for backwards compatibility.
Finds computers in AD using designated OU as starting point.
Sends ping to verify host is online before querying WMI.
Exports results to CSV.
Having an issue with a couple of PCs where access to WMI is being denied. In these cases the fields for Model, Serial, Manufacturer will be blank instead of "Not Found". I imagine it is an issue with WMI on those workstations and will have to investigate.