VMware Cloud Community
riksyours
Contributor
Contributor
Jump to solution

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

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.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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.


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

View solution in original post

0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What does this return?


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


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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

Giving the list of servers which is in vmnames.txt

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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

DTL24DEV58

DTL24DEV59

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

LucD
Leadership
Leadership
Jump to solution

That is not possible.

This

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

should return this

DTL24DEV58|DTL24DEV59


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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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

0 Kudos
riksyours
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

riksyours
Contributor
Contributor
Jump to solution

It Worked, Many Thanks once again.

0 Kudos