Hi Lucd,
that worked, but how I add a row for Operating System in the existing csv, its now working
$reportlocation1 = ".\POC_WIN_VM_OS_Info.csv"
$report1 = @()
$reportNotFound = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$found = $false
try {
$os = Get-WmiObject -ComputerName $row.Name -Class win32_operatingsystem -erroraction stop
$sys = Get-WmiObject -ComputerName $row.Name -Class Win32_ComputerSystem
$net = Get-WmiObject -ComputerName $row.Name -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPaddress } | Select-Object -ExpandProperty IPaddress | Select-Object -First 1 | Where-Object { $_ -notmatch ':' }
$DeviceInfo = @{}
$DeviceInfo.add("Hostname", $sys.DNSHostName)
$DeviceInfo.add("OperatingSystem", $os.name.split("|")[0])
#$DeviceInfo.add("IP Address", ($net.IPAddress -join (", ")))
$DeviceInfo.add("IP Address", $net)
$report += New-Object PSObject -Property $DeviceInfo | Select-Object "Hostname", "IP Address", "Operating System"
$report1 += $row | Add-Member -MemberType NoteProperty -Name 'OS Edition' -Value $($os.name.split("|")[0]) -PassThru
$found = $true
} catch {
}
if (-not $found) {
$reportNotFound += $row
}
}
#Write-Verbose ($report1 | Out-String) -Verbose
$report
$report1
$report1 | Export-CSV $reportlocation1 -NoTypeInformation
$reportNotFound | ft -auto
$reportNotFound | Export-Csv -Path ".\Error_VMs_Info.csv" -NoTypeInformation -UseCulture