ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell for Snapshot Management

    IT Discussion
    powershell vsphere esxi
    3
    3
    462
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • GreyG
      Grey
      last edited by Romo

      3 scripts to help you manage your snapshots:
      I think I posted this one before, but I'm including it for completeness.
      take-SnapsInteractively.ps1

      #############################
      #
      # Simple process to take a list of vm guests and take snapshots.
      # VM Guests lists one per line.
      #
      # -- Grey Howe Nov 5 2020
      #
      #############################
      
      #############################
      # Variables
      #
      # Change this to your VSphere server name:
      $viserver = ""
      # Use your favorite format or leave as is:
      $date=get-date -format FileDate
      #
      # End Variables
      #############################
      
      cls
      Write-Host -ForegroundColor cyan "------------------------------------------------------------`nMake snapshots based on a list in a file.`n------------------------------------------------------------"
      
      $file = "$($ENV:USERPROFILE)\desktop\systems.txt"
      
      do{Write-Host -ForegroundColor Cyan "Is your file ready?"
      $ready = (Read-Host -prompt "(q)uit, (Y)es, (n)o").ToLower()} while ($ready -notin @('q','y','n',''))
      
      if($ready -eq ''){$ready = 'y'}
      if($ready -eq 'n'){
          $nptest = Test-Path 'C:\Program Files\Notepad++\notepad++.exe'
              if($nptest -eq $true){
              Write-Host -ForegroundColor Cyan "Edit your file and rerun when ready."
              & "C:\Program Files\Notepad++\notepad++.exe" $file}
              else{
              & $env:windir\system32\notepad.exe $file
              throw "You're not ready yet. Ending."
              }
              }
      
      if($ready -eq 'y'){
      
          try {$vmlist = Get-VM |where {$_.guestid -like "dev" -and $_.Name -notlike "*replica*"}
          }
      
          catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException]{
              write-host -ForegroundColor yellow "Caution: No VIServer connection present. Please wait..."
              Connect-VIServer -Server $viserver -verbose:$false -WarningAction 0
              $vmlist = Get-VM |where {$_.guestid -like "dev" -and $_.Name -notlike "*replica*"}
              }
          
          $systems = get-content $file
      
          # Run in test mode?
              do{Write-Host -ForegroundColor cyan "`nRun in test mode? " -NoNewline
              $test = (Read-Host -prompt "(Y/n)").ToLower()} while ($test -notin @('y','n',''))
      
          #Fix 'default' and proceed to test mode.
       if($test -eq ''){$test = 'y'
      
              # test mode
              Write-Host -ForegroundColor Green "Test mode on the following systems:"
              $systems|write-host -ForegroundColor Green
              # Name the new snaps automatically, without asking, just to show work.
              $name = "$($env:USERNAME)-test"
      
              foreach($guest in $systems){
                  get-vm -Name $guest|ft
                  New-Snapshot -vm $guest -name $name -Verbose -whatif
              }
          # Exit Test Mode
          }
      
            ###### Active mode
      
          elseif($test -eq 'n'){
              Write-Host -ForegroundColor Yellow "Caution: Proceeding to make real snaps."
              $systems|write-host -ForegroundColor Yellow
              # Establish what to name the new snaps.
              Write-Host -ForegroundColor Cyan "Enter name for snapshots.`nENTER for '$($env:USERNAME)':" -NoNewline
              $name = read-host
              Write-Host -ForegroundColor Cyan "Purpose & requestor of these snapshots: " -NoNewline
              $reason = Read-Host
      
              # Fix 'default' name.
              if($name -eq ''){$name = "$($env:USERNAME)-snap"}
      
              foreach($guest in $systems){
              get-vm -Name $guest|ft
              New-Snapshot -vm $guest -name $name -Verbose -Description "$($env:USERNAME)-$($reason)" -RunAsync
              }
          }
      }
      # Quit
      if($ready -eq 'q'){
      Write-Host -ForegroundColor Cyan "Quitting."
      }
      

      get-SnapList.ps1

      ###### Variables
      # Change this to your VSphere server name:
      $viserver = ""
      ###### End Variables
      
      try {$vmsnaps = get-vm | Get-Snapshot| ?{$_.name -notlike "*veeam*" -and $_.name -and $_.name -notlike "*rest*" -and $_.name -notlike "*repl*"}|select vm,name,description,created,@{l='SizeInGB';e={($_.SizeGB).tostring("#.#")}} |sort created|ft -AutoSize
          $vmsnaps}
      
      catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException]{
          write-host -ForegroundColor yellow "Caution: No VIServer connection present. Please wait..."
          Connect-VIServer -Server $viserver -verbose:$false -WarningAction 0
          $vmsnaps = get-vm | Get-Snapshot| ?{$_.name -notlike "*veeam*" -and $_.name -and $_.name -notlike "*rest*" -and $_.name -notlike "*repl*"}|select vm,name,description,created,@{l='SizeInGB';e={($_.SizeGB).tostring("#.#")}} |sort created|ft -AutoSize
          $vmsnaps}
      

      remove-snapsInteractively.ps1

      ###### Variables
      # Change this to your VSphere server name:
      $viserver = ""
      ###### End Variables
      $l = 0
      try {$vmsnaps = get-vm | Get-Snapshot| ?{$_.name -notlike "*veeam*" -and $_.name -and $_.name -notlike "*rest*" -and $_.name -notlike "*repl*"}
          }
      
      catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException]{
          write-host -ForegroundColor yellow "Caution: No VIServer connection present. Please wait..."
          Connect-VIServer -Server $viserver -verbose:$false -WarningAction 0
          $vmsnaps = get-vm | Get-Snapshot| ?{$_.name -notlike "*veeam*" -and $_.name -and $_.name -notlike "*rest*" -and $_.name -notlike "*repl*"}
          }
      
      # Add 'Line' numbers to the array.
      $vmsnaps | %{$_ | Add-Member -NotePropertyName Line -NotePropertyValue $l -Force; $l++}
      # Display the list of snapshots, sorted by age.
      $vmsnaps |select Line,vm,name,description,created,@{l='SizeInGB';e={($_.SizeGB).tostring("#.#")}} |sort created|ft -AutoSize
      
      do {
      # Determine, by input, which snapshot to eliminate.
      $remLine = Read-Host -Prompt "Enter 'Line' number to remove"
      # Remove the requested snapshot.
      Remove-Snapshot $vmsnaps[$remLine] -runasync
      $quit = Read-host -prompt "Remove another?"
      } until ($quit -eq 'n')
      Write-host -ForegroundColor Cyan "`nEnd of Line."
      

      This last one isn't as 'pretty' as the other 2, but it gets the job done. Enjoy!

      1 Reply Last reply Reply Quote 4
      • DustinB3403D
        DustinB3403
        last edited by

        Just adding this detail.

        This is for ESXi / vSphere setups since there aren't any tags.

        RomoR 1 Reply Last reply Reply Quote 0
        • RomoR
          Romo @DustinB3403
          last edited by

          @DustinB3403 Tags added 😃

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post