Answered by:
WMI to export computer info and still list computers that return errors

-
I'm trying to create a WMI query to pull the Computer name, manufacturer, model, System Type, and Username but what I've made doesn't quite give me the right output. If I do from a batch script, it works, but the output doesn't include any computers that were unreachable.
I tried modifying a script I made for pulling AD info that outputs in the way I need it, but it's not quite right...
See below for the PowerShell and batch versions I made.
Batch:
wmic /node:@d:\input\getmodels.txt /output:"D:\output\listmodels_go.csv" computersystem get model,name,manufacturer,systemtype,Username /format:csv
PowerShell:
$Missing = New-Object psobject $Missing | Add-Member Noteproperty name("No Data") $Missing | Add-Member Noteproperty manufacturer("No Data") $Missing | Add-Member Noteproperty model("No Data") $Missing | Add-Member Noteproperty SystemType("No Data") $Missing | Add-Member Noteproperty Username("No Data") $Computers = Import-Csv C:\Powershell\WMIQueryInput.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') ForEach ($Computer in $Computers) { $DeviceName = $Computer.Name Get-WmiObject Win32_Computersystem -ComputerName $DeviceName | Format-List Name,manufacturer,model,SystemType,Username | Select Name,manufacturer,model,SystemType,Username If($Computer -ne $null){ $Computer | Export-Csv -Path "C:\Powershell\Logs\WMIQueryOutput.$CurrentDate.csv" -Append }else{ $Missing.name = $DeviceName $Missing | Export-Csv -Path "C:\Powershell\Logs\WMIQueryOutput.$CurrentDate.csv" -Append } }
- Edited by FFwarriorz Friday, February 9, 2018 6:03 PM wrong output file
Question
Answers
-
$template = @{ name = '' manufacturer = '' model = '' SystemType = '' Username = ''
Status = 'OffLine' } $Computers = Import-Csv C:\Powershell\WMIQueryInput.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') $Computers | ForEach-Object{ $hash = $template.Clone() $hash.name = $_.Name if ($system = Get-WmiObject Win32_Computersystem -ComputerName $_.Name) {
$hash.Status = 'Online' $hash.manufacturer = $system.Manufacturer $hash.model = $system.model $hash.systemType = $system.SystemType } [pscustomobject]$hash } | Export-Csv "C:\Powershell\Logs\EmployeeNumbers.$CurrentDate.csv"
\_(ツ)_/
- Edited by jrvModerator Friday, February 9, 2018 6:01 PM
- Marked as answer by FFwarriorz Friday, February 9, 2018 6:04 PM
All replies
-
$template = @{ name = '' manufacturer = '' model = '' SystemType = '' Username = ''
Status = 'OffLine' } $Computers = Import-Csv C:\Powershell\WMIQueryInput.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') $Computers | ForEach-Object{ $hash = $template.Clone() $hash.name = $_.Name if ($system = Get-WmiObject Win32_Computersystem -ComputerName $_.Name) {
$hash.Status = 'Online' $hash.manufacturer = $system.Manufacturer $hash.model = $system.model $hash.systemType = $system.SystemType } [pscustomobject]$hash } | Export-Csv "C:\Powershell\Logs\EmployeeNumbers.$CurrentDate.csv"
\_(ツ)_/
- Edited by jrvModerator Friday, February 9, 2018 6:01 PM
- Marked as answer by FFwarriorz Friday, February 9, 2018 6:04 PM
-
-
-
-