Automation

 View Only
  • 1.  Blank Output for unreachable VMs

    Posted Jun 17, 2021 11:43 AM

    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



  • 2.  RE: Blank Output for unreachable VMs

    Posted Jun 17, 2021 12:16 PM

    Do you mean that Error_VMs_Info.csv is empty?



  • 3.  RE: Blank Output for unreachable VMs

    Posted Jun 17, 2021 12:52 PM

    yes, both output file and error output file are empty



  • 4.  RE: Blank Output for unreachable VMs

    Posted Jun 17, 2021 01:20 PM

    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



  • 5.  RE: Blank Output for unreachable VMs

    Posted Jun 17, 2021 02:41 PM

    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



  • 6.  RE: Blank Output for unreachable VMs

    Posted Jun 17, 2021 02:59 PM

    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



  • 7.  RE: Blank Output for unreachable VMs

    Posted Jun 17, 2021 03:48 PM

    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"



  • 8.  RE: Blank Output for unreachable VMs
    Best Answer

    Posted Jun 17, 2021 06:50 PM

    Can you try like this?



  • 9.  RE: Blank Output for unreachable VMs

    Posted Jun 18, 2021 07:28 AM

    Thank you very much. that worked perfectly