Automation

 View Only
Expand all | Collapse all

I want combined report for 2 scripts into single report file.

  • 1.  I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 06:58 AM

    Script 1:

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    Get-View -ViewType VirtualMachine -Filter @{'Name'=$vmNames} |

    Select Name,

    @{N="Running OS";E={$_.Guest.GuestFullName}},

    @{N='VMware Toos Status';E={$_.Guest.ToolsStatus}},

    $vmNames |Export-Csv C:\report.csv -NoTypeInformation -UseCulture

    Script 2:

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    &{Get-VM $vmNames | ?{$_.PowerState -eq "PoweredOn"} | %{

        $vmNames = $_.Name; Get-NetworkAdapter -VM $_ |

            select @{n="VMName"; e={$vmNames}},Name,NetworkName,ConnectionState} |

            ?{$_.ConnectionState.Connected -eq $false}} |

    Export-Csv C:\report.csv -NoTypeInformation -UseCulture

    At the moment both Script working fine but the result is separated into two different report.csv file.



  • 2.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 07:25 AM

    Try something like this

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } -PipelineVariable vm |

    ForEach-Object -Process {

       $vm.Config.Hardware.Device | where { $_ -is [VMware.Vim.VirtualEthernetCard] } |

       ForEach-Object -Process {

       $_ | Select @{N = 'VM'; E = { $vm.Name } },

       @{N = 'Running OS'; E = { $vm.Guest.GuestFullName } },

       @{N = 'VMware Toos Status'; E = { $vm.Guest.ToolsStatus } },

       @{N = 'vNIC'; E = { $_.DeviceInfo.Label } },

       @{N = 'Network'; E = { (Get-View -Id $_.Backing.Network -Property Name).Name } },

       @{N = 'ConnectionState'; E = { if ($_.Connectable.Connected) { 'Connected' }else { 'Not connected' } } }

       }

    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture



  • 3.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 09:43 AM

    HI LucD,

    I am getting below error and report is empty.

    PS C:\> Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } -PipelineVariable vm |

    >>

    At line:1 char:87

    + ... e VirtualMachine -Filter @{'Name' = $vmNames } -PipelineVariable vm |

    + ~

    An empty pipe element is not allowed.

        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

        + FullyQualifiedErrorId : EmptyPipeElement

    PS C:\> ForEach-Object -Process {

    >>

    >>    $vm.Config.Hardware.Device | where { $_ -is [VMware.Vim.VirtualEthernetCard] } |

    >>

    >>    ForEach-Object -Process {

    >>

    >>    $_ | Select @{N = 'VM'; E = { $vm.Name } },

    >>

    >>    @{N = 'Running OS'; E = { $vm.Guest.GuestFullName } },

    >>

    >>    @{N = 'VMware Toos Status'; E = { $vm.Guest.ToolsStatus } },

    >>

    >>    @{N = 'vNIC'; E = { $_.DeviceInfo.Label } },

    >>

    >>    @{N = 'Network'; E = { (Get-View -Id $_.Backing.Network -Property Name).Name } },

    >>

    >>    @{N = 'ConnectionState'; E = { if ($_.Connectable.Connected) { 'Connected' }else { 'Not connected' } } }

    >>

    >>    }

    >>

    >> } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture



  • 4.  RE: I want combined report for 2 scripts into single report file.
    Best Answer

    Posted Jul 12, 2019 09:47 AM

    Which PowerShell version are you using?

    What does $PSVersionTable say?
    The PipelineVariable has been introduced in PS v4.

    Try like this

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } |

    ForEach-Object -Process {

       $vm = $_

       $vm.Config.Hardware.Device | where { $_ -is [VMware.Vim.VirtualEthernetCard] } |

       ForEach-Object -Process {

       $_ | Select @{N = 'VM'; E = { $vm.Name } },

       @{N = 'Running OS'; E = { $vm.Guest.GuestFullName } },

       @{N = 'VMware Toos Status'; E = { $vm.Guest.ToolsStatus } },

       @{N = 'vNIC'; E = { $_.DeviceInfo.Label } },

       @{N = 'Network'; E = { (Get-View -Id $_.Backing.Network -Property Name).Name } },

       @{N = 'ConnectionState'; E = { if ($_.Connectable.Connected) { 'Connected' }else { 'Not connected' } } }

       }

    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

    ---------------------------------------------------------------------------------------------------------

    Was it helpful? Let us know by completing this short survey here.



  • 5.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 09:55 AM

    PS C:\> $PSVersionTable

    Name                           Value

    ----                           -----

    PSVersion                      5.1.14393.2969

    PSEdition                      Desktop

    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

    BuildVersion                   10.0.14393.2969

    CLRVersion                     4.0.30319.42000

    WSManStackVersion              3.0

    PSRemotingProtocolVersion      2.3

    SerializationVersion           1.1.0.1



  • 6.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 09:56 AM

    PS C:\> Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } |

    >>

    At line:1 char:66

    + Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } |

    +                                                                  ~

    An empty pipe element is not allowed.

        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

        + FullyQualifiedErrorId : EmptyPipeElement



  • 7.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 09:59 AM

    What does this return?


    (Get-Content -Path .\vmnames.txt) -join '|'



  • 8.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 10:02 AM

    Giving the list of servers which is in vmnames.txt



  • 9.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 10:04 AM

    I have run this successfully but it is generating empty report file.

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } |

    ForEach-Object -Process {

       $vm = $_

       $vm.Config.Hardware.Device | where { $_ -is [VMware.Vim.VirtualEthernetCard] } |

       ForEach-Object -Process {

       $_ | Select @{N = 'VM'; E = { $vm.Name } },

       @{N = 'Running OS'; E = { $vm.Guest.GuestFullName } },

       @{N = 'VMware Toos Status'; E = { $vm.Guest.ToolsStatus } },

       @{N = 'vNIC'; E = { $_.DeviceInfo.Label } },

       @{N = 'Network'; E = { (Get-View -Id $_.Backing.Network -Property Name).Name } },

       @{N = 'ConnectionState'; E = { if ($_.Connectable.Connected) { 'Connected' }else { 'Not connected' } } }

       }

    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture



  • 10.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 11:22 AM

    How are you running the script?
    Your previous error seems to indicate that you copy/paste the script line by line to the PS prompt.
    That will not work, since the following line will complain about an empty pipeline, if run on it's own.

    Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } |

    You have to copy/paste the complete script in one go.
    Or better yet, store it in a .ps1 file, and then run the .ps1 file.



  • 11.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 11:38 AM

    Many Thanks LucD, it worked what i was looking for using .ps1 file.



  • 12.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 16, 2019 05:18 AM

    HI LucD,

    In the below script can i have two modifications.

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames } |

    ForEach-Object -Process {

       $vm = $_

       $vm.Config.Hardware.Device | where { $_ -is [VMware.Vim.VirtualEthernetCard] } |

       ForEach-Object -Process {

       $_ | Select @{N = 'VM'; E = { $vm.Name } },

       @{N = 'Running OS'; E = { $vm.Guest.GuestFullName } },

       @{N = 'VMware Toos Status'; E = { $vm.Guest.ToolsStatus } },

       @{N = 'vNIC'; E = { $_.DeviceInfo.Label } },

       @{N = 'Network'; E = { (Get-View -Id $_.Backing.Network -Property Name).Name } },

       @{N = 'ConnectionState'; E = { if ($_.Connectable.Connected) { 'Connected' }else { 'Not connected' } } }

       }

    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

    1:I want only those machine output which are PoweredOn

    2: In Network Connection State, i want both results for Adapter state and Connect at PowerOn state.

    Thanks in advance.



  • 13.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 16, 2019 06:04 AM

    Sure, try like this

    $vmNames = (Get-Content -Path .\vmnames.txt) -join '|'

    Get-View -ViewType VirtualMachine -Filter @{'Name' = $vmNames; 'Runtime.PowerState' = 'poweredOn' } |

    ForEach-Object -Process {

       $vm = $_

       $vm.Config.Hardware.Device | where { $_ -is [VMware.Vim.VirtualEthernetCard] } |

       ForEach-Object -Process {

       $_ | Select @{N = 'VM'; E = { $vm.Name } },

       @{N = 'Running OS'; E = { $vm.Guest.GuestFullName } },

       @{N = 'VMware Toos Status'; E = { $vm.Guest.ToolsStatus } },

       @{N = 'vNIC'; E = { $_.DeviceInfo.Label } },

       @{N = 'Network'; E = { (Get-View -Id $_.Backing.Network -Property Name).Name } },

       @{N = 'ConnectionState'; E = { if ($_.Connectable.Connected) { 'Connected' }else { 'Not connected' } } },

       @{N = 'ConnectAtPowerOn'; E = { $_.Connectable.StartConnected } }

       }

    }

    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture



  • 14.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 16, 2019 07:14 AM

    It Worked, Many Thanks once again.



  • 15.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 11:09 AM

    That's not what I asked.
    How does it show those names?



  • 16.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 11:21 AM

    PS C:\> (Get-Content -Path .\vmnames.txt) -join '|'

    DTL24DEV58

    DTL24DEV59



  • 17.  RE: I want combined report for 2 scripts into single report file.

    Posted Jul 12, 2019 11:22 AM

    That is not possible.

    This

    PS C:\> (Get-Content -Path .\vmnames.txt) -join '|'

    should return this

    DTL24DEV58|DTL24DEV59