VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Blank Output for unreachable VMs

Hi,

I am unable to validate and get the output for unreachable or connection issue VMs to csv file.

Please help

$reportlocation1 = ".\POC_WIN_VM_OS_Info.csv"
$reportlocation2 = ".\POC_WIN_VM_OS_Info88.csv"

$report1 = @()
$reportNotFound = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$found = $false
try {
$os = Get-WmiObject -computername $object.("Name") -class win32_operatingsystem
$sys = Get-WmiObject -computername $object.("Name") -class Win32_ComputerSystem
$net = Get-WmiObject -computername $object.("Name") -class Win32_NetworkAdapterConfiguration | where{$_.IPaddress} | select -expandproperty IPaddress | Select -First 1 | where{$_ -notmatch ':'}

$DeviceInfo= @{}
$DeviceInfo.add("Hostname", $sys.DNSHostName)
$DeviceInfo.add("Operating System", $os.name.split("|")[0])
$DeviceInfo.add("IP Address", ($net.IPAddress -join (", ")))
$report1 += New-Object PSObject -Property $DeviceInfo | Select-Object "Hostname", "IP Address", "Operating System"
$found = $true
}
catch {
}
$result
if(-not $found){
$reportNotFound += $row
}
}

Write-Verbose ($report1 | Out-String) -Verbose
$report1 | Export-CSV $reportlocation2 -NoTypeInformation
$reportNotFound | ft -auto
$reportNotFound | Export-Csv -Path ".\Error_VMs_Info.csv" -NoTypeInformation -UseCulture

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try like this?

$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 ':' }

      $report1 += $row | 
        Add-Member -MemberType NoteProperty -Name 'OS Edition' -Value $($os.name.split("|")[0]) -PassThru |
        Add-Member -MemberType NoteProperty 'IP Address' -Value $net -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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Do you mean that Error_VMs_Info.csv is empty?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

yes, both output file and error output file are empty

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are again using a variable that is not initialised for ComputerName.
And you have to specify an ErrorAction to get into the Catch block.

Something like this

$reportlocation1 = ".\POC_WIN_VM_OS_Info.csv"
$reportlocation2 = ".\POC_WIN_VM_OS_Info88.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("Operating System", $os.name.split("|")[0])
    $DeviceInfo.add("IP Address", ($net.IPAddress -join (", ")))
    $report1 += New-Object PSObject -Property $DeviceInfo | Select-Object "Hostname", "IP Address", "Operating System"
    $found = $true
  } catch {
  }
  if (-not $found) {
    $reportNotFound += $row
  }
}

write-host "=== Report"
$report1
write-host "==== Not found"
$reportNotFound


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure I'm following.
How do you want that CSV to look?
Perhaps show a mockup of what you want to see in there


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

import CSV has following

Folder, Name, "IP Address"

I would like to add the Operating System appended to the same CSV in the output

Folder, Name, "IP Address", "Operating System"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try like this?

$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 ':' }

      $report1 += $row | 
        Add-Member -MemberType NoteProperty -Name 'OS Edition' -Value $($os.name.split("|")[0]) -PassThru |
        Add-Member -MemberType NoteProperty 'IP Address' -Value $net -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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much. that worked perfectly 🙂

0 Kudos