Automation

 View Only
Expand all | Collapse all

Need a script that lists VM with a snapshot

  • 1.  Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 12:54 PM

    Hi,

    I am fairly new to the powershell scripting and even newer to the vSphere CLI. I have no problem reading and understanding scripts, but making one is an whole other story. Anyway,  I am looking for help with creating a script that will check all the vm's on all the ESX Hosts, if they have a snapshot and if they do echo the VM name on screen.

    This is what I have so far but I am stuck and probably on the wrong track. Any help/suggestions are more than welcome!

    $user = "<removed>"
    $passw = "<removed>"
    $VIServers = "<IP ESXi Host>", "<IP ESX Host1>", "<IP ESX Host2>", "<IP ESX Host3>", "<IP ESX Host4>", "<IP ESX Host5>", "<IP ESX Host6>"

    foreach ($VIServer in $ViServers) {
      Connect-VIServer -Server $VIServer -user $user -Password $passw
      $vms = get-vmhost $VIServer | get-vm

      foreach ($vm in $vms ) {
       <code which checks to see if there is a snapshot and echo's the VM name on screen. Clueless>

      }
    }
    }

    In forward thanks!

    Regards,

    Erik



  • 2.  RE: Need a script that lists VM with a snapshot
    Best Answer

    Posted Dec 30, 2010 01:08 PM

    Why directly to the ESX:es? .. Connect to vCenter instead and then it´s very easy..

    Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created

    -Kenth



  • 3.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 01:29 PM

    Or if you want to solve it without vCenter..

    $strEsx = @("s-esxt0100.int.lio.se","s-esxt0200.int.lio.se")
    $strEsx | foreach-object {connect-viserver $_
    Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created}

    Hope it gets you going a bit .. =)

    - Kenth



  • 4.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 01:37 PM

    I think you can make it a bit faster by doing it like this

    Get-Snapshot -VM (Get-VM) | Select VM,Name,Description,Created | Sort Created



  • 5.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 01:43 PM

    You are probably right! =)



  • 6.  RE: Need a script that lists VM with a snapshot

    Broadcom Employee
    Posted Dec 30, 2010 01:37 PM

    It is recommended to connect to vCenter if you have that available.

    In case you don't have vCenter, you need to connect to all ESX hosts directly.

    You can do this with one call to the Connect-VIServer cmdlet however, as I've demonstrated in the code below:

    $user = "<removed>"
    $passw = "<removed>"
    $VIServers = "<IP ESXi Host>", "<IP ESX Host1>", "<IP ESX Host2>", "<IP ESX Host3>", "<IP ESX Host4>", "<IP ESX Host5>", "<IP ESX Host6>"
    
    Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -Confirm:$false
    Connect-VIServer $VIServers -user $user -Password $passw
    Get-VM | Get-Snapshot | Select vm,name,description,powerstate,created,sizeMB,@{N="DaysOld";E={((Get-date)-$_.created).Days}}
    Disconnect-VIServer $VIServers -Confirm:$false

    Arnim



  • 7.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:02 PM

    First of all: Thanks guys! All your input helped me a lot!

    There is one weird thing though. If I run this "Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created" from the script the onscreen output is different from when I run the exact same command in the CLI directly

    Output when ran from script

    VM          : SVR-AS-038
    Name        : script test
    Description :
    Created     : 12/30/2010 11:44:16 AM


    VM          : SVR-IS-032
    Name        : script test 2
    Description :
    Created     : 12/30/2010 2:57:53 PM


    Output when ran from CLI directly

    VM                  Name                Description         Created
    --                  ----                -----------         -------
    SVR-AS-038          script test                             12/30/2010 11:44...
    SVR-IS-032          script test 2                           12/30/2010 2:57:...


    I prefer the latter. Is this possible when using a script?



  • 8.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:05 PM

    Sure, pipe the output to a Format-Table cmdlet (alias ft)

    Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created | ft -Autosize



  • 9.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:12 PM

    LucD you are scaringly quick! You almost replied before I even asked the question! Loving it!  :smileyhappy:

    But it doesn't work :smileysad: I get this error:

    out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.    + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand


  • 10.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:15 PM

    Try my thingy ..

    pipe Out-Null in the connection string

    ie..

    Connect-viserver MYSERVER |Out-Null

    -Kenth



  • 11.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:22 PM

    @gekko (you're awesome too :smileyhappy:)

    Sorry, I was busy testing the solution provided by LucD when you replied. Did not see your reply until I submitted mine. Nevertheless, your solution does work! Excellent!

    @Armin

    Same error as the solution provided by LucD, not that strange as it is the same solution :smileysilly:

    And then there is another challenge. Should I create a new thread or just post it anyway?

    I want to schedule another VSphere PowerCLI script, but that doesn't work because I first have to start/load the vSphere PowerCLI before I can run this script (This script checks every VM if there is a CD image mounted, if so the image is dismounted and the CD is disconnected. This is because of a known issue with Backup Exec). Any ideas on this one?



  • 12.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:29 PM

    LOL, thanks m8!

    Lets see if i got it right .. :

    In the Program/script box you enter :

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

    In the arguments box :

    "& 'D:\Script\Script.ps1'"

    This is in Task Scheduler ..

    -Kenth

    Edit: forgot a ' in

    "& 'D:\Script\Script.ps1'"



  • 13.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:33 PM

    problem is that de vSphere PowerCLI isn't loaded by default in powershell, so the script fails using your procedure. I first have to find a way to load up the vSphere PowerCLI and then run the script.. do I maybe have to reconfigure powershell?



  • 14.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 04:06 PM

    You can add a Add-PSSnapin cmdlet to one of the PS profile files



  • 15.  RE: Need a script that lists VM with a snapshot

    Posted Dec 31, 2010 11:46 AM

    Hi guys!

    Thanks for all your help, all scripts are working the way we want!

    Only one note regarding the scheduling, I had to add  -command "C:\scripts\script.ps1" to the "Add arguments (optional):" field, instead of

    "& 'D:\Script\Script.ps1'"

    Happy new year!

    Regards,

    Erik



  • 16.  RE: Need a script that lists VM with a snapshot

    Broadcom Employee
    Posted Dec 30, 2010 03:17 PM

    Add

    | Format-Table -AutoSize

    To the end of the code.

    Arnim



  • 17.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:09 PM

    or add pipe "Out-Null" after the connection to vCenters ..

    $strEsx | foreach-object {connect-viserver $_ |Out-Null

    -Kenth



  • 18.  RE: Need a script that lists VM with a snapshot

    Posted Dec 30, 2010 03:35 PM

    ahh okey!

    Add this to your script, at the top :

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue