VMware Cloud Community
MrVmware9423
Expert
Expert
Jump to solution

PowerCLI script to find disconnected VMs

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

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

11 Replies
mattboren
Expert
Expert
Jump to solution

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?

MrVmware9423
Expert
Expert
Jump to solution

will it work  if i connect standalone esx host or i have to connect vcenter server ????

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, again-

Yes, this should work either way -- with your PowerCLI session connected to a vCenter server, or directly to an ESX(i) host.

0 Kudos
MrVmware9423
Expert
Expert
Jump to solution

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.

0 Kudos
lenzker
Enthusiast
Enthusiast
Jump to solution

just put

> Export-Csv "C:\networkinfo.txt" -NoTypeInformation  

behind the code

VCP,VCAP-DCA,VCI -> https://twitter.com/lenzker -> http://vxpertise.net
LucD
Leadership
Leadership
Jump to solution

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

hemadri18
Contributor
Contributor
Jump to solution

ConnectionState.Connected is not working..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
hemadri18
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
hemadri18
Contributor
Contributor
Jump to solution

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 ??

0 Kudos