Dear team,
In my environment many vms are poweredon but not connected on a network, i want powercli script which list these kind of vms which r poweredon but no connected on a network. Request you to help me with the same.
regards
Mr VMware
That might be a problem with the foreach loop, that normally doesn't place objects in the pipeline.
Execute the code as a block and pipe that to the Export-Csv
&{Get-VM | ?{$_.PowerState -eq "PoweredOn"} | %{ $strVMName = $_.Name; Get-NetworkAdapter -VM $_ |
select @{n="VMName"; e={$strVMName}},Name,NetworkName,ConnectionState} |
?{$_.ConnectionState.Connected -eq $false}} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello, MrVmware9423-
If you are talking about network adapters that are "not connected", you can use something like:
Get-VM | ?{$_.PowerState -eq "PoweredOn"} | %{
$strVMName = $_.Name; Get-NetworkAdapter -VM $_ | `
select @{n="VMName"; e={$strVMName}},Name,NetworkName,ConnectionState
} | ?{$_.ConnectionState.Connected -eq $false}
That gets all powered on VMs, gets their network adapters, and returns info about ones whose ConnectionState.Connected property is $false. The output should be something like:
VMName Name NetworkName ConnectionState ------ ---- ----------- --------------- dVM0 Network adapter 1 VM Network NotConnected, NoGuestControl, StartConnected dVM8 Network adapter 3 someOtherNet NotConnected, NoGuestControl, StartConnected ...
How does that do for you?
will it work if i connect standalone esx host or i have to connect vcenter server ????
Hello, again-
Yes, this should work either way -- with your PowerCLI session connected to a vCenter server, or directly to an ESX(i) host.
Thanks frined it's working very much fine now, but i want to export output into a text file, please help me with the same.
just put
> Export-Csv "C:\networkinfo.txt" -NoTypeInformation
behind the code
That might be a problem with the foreach loop, that normally doesn't place objects in the pipeline.
Execute the code as a block and pipe that to the Export-Csv
&{Get-VM | ?{$_.PowerState -eq "PoweredOn"} | %{ $strVMName = $_.Name; Get-NetworkAdapter -VM $_ |
select @{n="VMName"; e={$strVMName}},Name,NetworkName,ConnectionState} |
?{$_.ConnectionState.Connected -eq $false}} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ConnectionState.Connected is not working..
Would you mind to expand on that ?
Is there an error ? Or is the script not filtering out the correct objects ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi
Thanks 4 replying. Script is doing nothing just typing the whole cmd as output.
Whn I remove the connectionsate.connected condition it give whole list of vm as output.
Need to clarify connectionstate is a list?
Thanks
Strange, would you mind attaching screenshot of both runs at the PowerCLI command prompt.
It looks like something might be missing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sorry for late responce..
I have types the following in the script
Get-VM | ?{$_.'PowerState' -eq "PoweredOn"} | %{
Get-NetworkAdapter -VM $_ |select @{n="VMSate"; e={$_.ConnectionState}}} | ?{$_.ConnectionState.Connected -eq $false} | out-file "C:\VM.txt"
and output file is empty..
There is one more basic question is
$_.ConnectionState has the value GuestControl, Connected, StartConnected
Is the $_.ConnectionState is list ??