Hi,
I'm having problem with a script that is a simple WMI Inventory script that searches Active Directory for computer accounts and tests the connection and should go through checking wmi queries the problem is, i'm only getting the Computer Name, IP & MAC Address it's not going through the WMI queries.
Can anyone see anything incorrect.
# ///////// CSV Output File ///////////
$outputFile='liveaudit.csv'
# ////////// Input File //////////////
$logFile='Computers.txt'
# ///////// LDAP Path //////////////
$root='LDAP://DC=The7Stars,DC=local'
# //////// AD Pre-Req ////////////
$a= [adsisearcher]"objectcategory=computer"
$a.SearchRoot=$root
$a.PageSize= 200
$a.PropertiesToLoad.Add("name") | Out-Null
$a.PropertiesToLoad.Add("distinguishedname") | Out-Null
$a.PropertiesToLoad.Add("samaccountname") | Out-Null
$results=$a.findall()
$ADObjects= @()
foreach($resultin$results) {
[Array]$propertiesList=$result.Properties.PropertyNames
$obj=New-Object PSObject
foreach($propertyin$propertiesList) {
$obj | add-member-membertype noteproperty -name$property-value ([string]$result.Properties.Item($property))
} # end foreach
$ADObjects+=$obj
} # end foreach
# Create Log File
New-Item-ItemType File -Path$logFile-Force | Out-Null
$results= @()
# Scan Computer Objects
foreach ($compin$ADObjects) {
$result=New-Object psObject
Write-Host"Querying $($comp.name)"-ForegroundColor green
$result | Add-Member-MemberType NoteProperty -Name'Network Name'-Value ($comp.SamAccountName-replace'\$$')
$result | Add-Member-MemberType NoteProperty -Name'Name'-Value ($comp.Name)
$result | Add-Member-MemberType NoteProperty -Name'DN'-Value$comp.distinguishedname
try {
# IP Queries
$IPAddr=Test-Connection-ComputerName$comp.name-Count 1 -ErrorActioncontinue | select -ExpandProperty ipv4Address | select -ExpandProperty ipAddressToString
$result | Add-Member-MemberType NoteProperty -Name'IP Address'-Value$IPAddr
# MAC address query
$mac= gwmi -Class win32_networkadapterconfiguration -ComputerName$comp.name-eacontinue | `
Where-Object {$_.macaddress-and ($_.ipaddress-contains$IPAddr-or$_.ipaddress-eq$IPAddr)} | select -Expand macaddress
$result | Add-Member-MemberType NoteProperty -Name'MAC Address'-Value$mac
# Other WMI queries
$computerSystem= gwmi -Class win32_computersystem -ComputerName$comp.name
$OperatingSystem= gwmi -Class win32_operatingsystem -ComputerName$comp.name
$Processor= @(gwmi -Class win32_processor -ComputerName$comp.name)
$RAM= [math]::round(($computerSystem.TotalPhysicalMemory)/1mb,3)
$bios= gwmi -Class win32_bios -ComputerName$comp.name
$biosDate= [Management.ManagementDateTimeConverter]::ToDateTime($bios.ReleaseDate)
$OS_InstallDate= [Management.ManagementDateTimeConverter]::ToDateTime($OperatingSystem.InstallDate)
$C_Drive= gwmi -Class win32_logicaldisk -ComputerName$comp.name | ? { $_.DeviceID-eq'C:' }
# Add results of WMI queries
$result | Add-Member-MemberType NoteProperty -Name'Manufacturer'-Value$computerSystem.Manufacturer
$result | Add-Member-MemberType NoteProperty -Name'Product Description'-Value$computerSystem.Model
$result | Add-Member-MemberType NoteProperty -Name'Operating System'-Value ($OperatingSystem.Name-split'\|')[0]
$result | Add-Member-MemberType NoteProperty -Name'OS_SP'-Value$OperatingSystem.csdVersion
$result | Add-Member-MemberType NoteProperty -Name'OS_InstallDate'-Value$OS_InstallDate
$result | Add-Member-MemberType NoteProperty -Name'CPU'-Value$Processor[0].name
$result | Add-Member-MemberType NoteProperty -Name'ProcSpeed'-Value$Processor[0].maxclockspeed
$result | Add-Member-MemberType NoteProperty -Name'ProcCount'-Value$Processor.count
$result | Add-Member-MemberType NoteProperty -Name'Memory'-Value$RAM
$result | Add-Member-MemberType NoteProperty -Name'BIOSDate'-Value$biosDate
$result | Add-Member-MemberType NoteProperty -Name'Hard Drive Capacity'-Value ([math]::Round(($C_Drive.size)/1gb,3))
$result | Add-Member-MemberType NoteProperty -Name'C_Space'-Value ([math]::Round(($C_Drive.freespace)/1gb,3))
} # end try
catch {
Write-Host"Could not contact $($comp.name)"-ForegroundColor Red
Out-File-FilePath$logFile-InputObject$comp.name-Append-Force
}
$results+=$result
} # end foreach $comp
$results | Export-Csv-Path$outputFile-NoTypeInformation-Force
$results | Select-Object-First 20 | Format-Table
Write-Host"$($results.count) computers processed. The results are stored in $outputFile"
$offlineCount= @(Get-Content$logFile).count
Write-Host"$offlineCount computers were offline. The list is stored in $logFile"
$outputFile='liveaudit.csv'
# ////////// Input File //////////////
$logFile='Computers.txt'
# ///////// LDAP Path //////////////
$root='LDAP://DC=The7Stars,DC=local'
# //////// AD Pre-Req ////////////
$a= [adsisearcher]"objectcategory=computer"
$a.SearchRoot=$root
$a.PageSize= 200
$a.PropertiesToLoad.Add("name") | Out-Null
$a.PropertiesToLoad.Add("distinguishedname") | Out-Null
$a.PropertiesToLoad.Add("samaccountname") | Out-Null
$results=$a.findall()
$ADObjects= @()
foreach($resultin$results) {
[Array]$propertiesList=$result.Properties.PropertyNames
$obj=New-Object PSObject
foreach($propertyin$propertiesList) {
$obj | add-member-membertype noteproperty -name$property-value ([string]$result.Properties.Item($property))
} # end foreach
$ADObjects+=$obj
} # end foreach
# Create Log File
New-Item-ItemType File -Path$logFile-Force | Out-Null
$results= @()
# Scan Computer Objects
foreach ($compin$ADObjects) {
$result=New-Object psObject
Write-Host"Querying $($comp.name)"-ForegroundColor green
$result | Add-Member-MemberType NoteProperty -Name'Network Name'-Value ($comp.SamAccountName-replace'\$$')
$result | Add-Member-MemberType NoteProperty -Name'Name'-Value ($comp.Name)
$result | Add-Member-MemberType NoteProperty -Name'DN'-Value$comp.distinguishedname
try {
# IP Queries
$IPAddr=Test-Connection-ComputerName$comp.name-Count 1 -ErrorActioncontinue | select -ExpandProperty ipv4Address | select -ExpandProperty ipAddressToString
$result | Add-Member-MemberType NoteProperty -Name'IP Address'-Value$IPAddr
# MAC address query
$mac= gwmi -Class win32_networkadapterconfiguration -ComputerName$comp.name-eacontinue | `
Where-Object {$_.macaddress-and ($_.ipaddress-contains$IPAddr-or$_.ipaddress-eq$IPAddr)} | select -Expand macaddress
$result | Add-Member-MemberType NoteProperty -Name'MAC Address'-Value$mac
# Other WMI queries
$computerSystem= gwmi -Class win32_computersystem -ComputerName$comp.name
$OperatingSystem= gwmi -Class win32_operatingsystem -ComputerName$comp.name
$Processor= @(gwmi -Class win32_processor -ComputerName$comp.name)
$RAM= [math]::round(($computerSystem.TotalPhysicalMemory)/1mb,3)
$bios= gwmi -Class win32_bios -ComputerName$comp.name
$biosDate= [Management.ManagementDateTimeConverter]::ToDateTime($bios.ReleaseDate)
$OS_InstallDate= [Management.ManagementDateTimeConverter]::ToDateTime($OperatingSystem.InstallDate)
$C_Drive= gwmi -Class win32_logicaldisk -ComputerName$comp.name | ? { $_.DeviceID-eq'C:' }
# Add results of WMI queries
$result | Add-Member-MemberType NoteProperty -Name'Manufacturer'-Value$computerSystem.Manufacturer
$result | Add-Member-MemberType NoteProperty -Name'Product Description'-Value$computerSystem.Model
$result | Add-Member-MemberType NoteProperty -Name'Operating System'-Value ($OperatingSystem.Name-split'\|')[0]
$result | Add-Member-MemberType NoteProperty -Name'OS_SP'-Value$OperatingSystem.csdVersion
$result | Add-Member-MemberType NoteProperty -Name'OS_InstallDate'-Value$OS_InstallDate
$result | Add-Member-MemberType NoteProperty -Name'CPU'-Value$Processor[0].name
$result | Add-Member-MemberType NoteProperty -Name'ProcSpeed'-Value$Processor[0].maxclockspeed
$result | Add-Member-MemberType NoteProperty -Name'ProcCount'-Value$Processor.count
$result | Add-Member-MemberType NoteProperty -Name'Memory'-Value$RAM
$result | Add-Member-MemberType NoteProperty -Name'BIOSDate'-Value$biosDate
$result | Add-Member-MemberType NoteProperty -Name'Hard Drive Capacity'-Value ([math]::Round(($C_Drive.size)/1gb,3))
$result | Add-Member-MemberType NoteProperty -Name'C_Space'-Value ([math]::Round(($C_Drive.freespace)/1gb,3))
} # end try
catch {
Write-Host"Could not contact $($comp.name)"-ForegroundColor Red
Out-File-FilePath$logFile-InputObject$comp.name-Append-Force
}
$results+=$result
} # end foreach $comp
$results | Export-Csv-Path$outputFile-NoTypeInformation-Force
$results | Select-Object-First 20 | Format-Table
Write-Host"$($results.count) computers processed. The results are stored in $outputFile"
$offlineCount= @(Get-Content$logFile).count
Write-Host"$offlineCount computers were offline. The list is stored in $logFile"
Result of the CSV is the following
"Network Name","Name","DN","IP Address","MAC Address""STAR-SBS1","STAR-SBS1","CN=STAR-SBS1,OU=Disabled Computers,DC=The7Stars,DC=local",,"STARS-RD1","STARS-RD1","CN=STARS-RD1,OU=RemoteComputers,OU=Tottenham Mews,DC=The7Stars,DC=local","192.168.25.9","00:0C:29:9F:F9:D1""STAR-SRV01","STAR-SRV01","CN=STAR-SRV01,OU=Domain Controllers,DC=The7Stars,DC=local","192.168.25.11","00:0C:29:65:72:F6"