PowerCLI

 View Only
Expand all | Collapse all

PowerCLI script to find disconnected VMs

  • 1.  PowerCLI script to find disconnected VMs

    Posted Jan 30, 2013 03:47 AM

    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



  • 2.  RE: PowerCLI script to find disconnected VMs

    Posted Jan 30, 2013 04:06 AM

    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?



  • 3.  RE: PowerCLI script to find disconnected VMs

    Posted Jan 30, 2013 04:10 AM

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



  • 4.  RE: PowerCLI script to find disconnected VMs

    Posted Jan 30, 2013 04:36 AM

    Hello, again-

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



  • 5.  RE: PowerCLI script to find disconnected VMs

    Posted Jan 30, 2013 08:33 AM

    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.



  • 6.  RE: PowerCLI script to find disconnected VMs

    Posted Jan 30, 2013 01:46 PM

    just put

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

    behind the code



  • 7.  RE: PowerCLI script to find disconnected VMs
    Best Answer

    Posted Jan 30, 2013 03:08 PM

    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


  • 8.  RE: PowerCLI script to find disconnected VMs

    Posted Sep 30, 2013 08:21 AM

    ConnectionState.Connected is not working..



  • 9.  RE: PowerCLI script to find disconnected VMs

    Posted Sep 30, 2013 09:37 AM

    Would you mind to expand on that ?

    Is there an error ? Or is the script not filtering out the correct objects ?



  • 10.  RE: PowerCLI script to find disconnected VMs

    Posted Sep 30, 2013 04:12 PM

    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



  • 11.  RE: PowerCLI script to find disconnected VMs

    Posted Sep 30, 2013 05:29 PM

    Strange, would you mind attaching screenshot of both runs at the PowerCLI command prompt.

    It looks like something might be missing.



  • 12.  RE: PowerCLI script to find disconnected VMs

    Posted Oct 03, 2013 08:32 AM

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