Automation

 View Only
Expand all | Collapse all
  • 1.  VM Snapshot

    Posted Oct 10, 2017 07:34 AM

    Hi,

    I am new for scripting.. I am trying to create script for Vm Snapshot for multiple VM.. But i need Snapshot should take vm name automatic and create snapshot like VM1_BEFOREPATCH , VM2_BEFOREPATCH and so on..

    #VM Server List

    $vmlist = Get-Content C:\Servers.txt

    foreach($VM in $VMlist) {

        New-Snapshot -VM $vm -Name BEFOREPATCH- -description 'Patching'

    }

    Disconnect-VIServer -Confirm:$false



  • 2.  RE: VM Snapshot
    Best Answer

    Posted Oct 10, 2017 07:58 AM

    You can use variable substitution in a double-quoted string for that.

    Something like this

    Connect-VIServer -Server YourVC

    #VM Server List

    $vmlist = Get-Content C:\Servers.txt

    foreach($VM in Get-VM $VMlist) {

        New-Snapshot -VM $vm -Name "$($vm.Name)_BEFOREPATCH" -description 'Patching'

    }

    Disconnect-VIServer -Confirm:$false 



  • 3.  RE: VM Snapshot

    Posted Oct 11, 2017 09:59 AM

    Thank you so much for the help..

    Now i would like to send mail of the created VM-Snapshot only from Server.txt . Can you help with this as i am getting all vm data.

    Connect-VIServer -Server YourVC

    #VM Server List

    $vmlist = Get-Content C:\Servers.txt

    foreach($VM in Get-VM $VMlist) {

        New-Snapshot -VM $vm -Name "$($vm.Name)_BEFOREPATCH" -description 'Patching'

    }

    Disconnect-VIServer -Confirm:$false

    # Mail Format

    $body = Get-VM | Get-Snapshot | Select Name, VM | Out-String

    Send-MailMessage -From "xyz.com" -To "abc.com" -SmtpServer "vmware.com" -Body $body -Subject "Snapshot Report"



  • 4.  RE: VM Snapshot

    Posted Oct 11, 2017 11:02 AM

    You could do

    # Mail Format

    $body = Get-VM -Name $vmList | Get-Snapshot | Select Name, VM | Out-String

    Send-MailMessage -From "xyz.com" -To "abc.com" -SmtpServer "vmware.com" -Body $body -Subject "Snapshot Report"



  • 5.  RE: VM Snapshot

    Posted Oct 11, 2017 12:28 PM

    Thanks i will try and let you know



  • 6.  RE: VM Snapshot

    Posted Oct 13, 2017 06:11 AM

    It works Perfectly thanks a lot



  • 7.  RE: VM Snapshot

    Posted Nov 02, 2017 08:41 PM

    Thanks for the reply.

    Is there any way that multiple user can execute the script at the same time with different server names ? as there is only 1 txt file fro server list.



  • 8.  RE: VM Snapshot

    Posted Nov 02, 2017 08:50 PM

    You mean by using different text files?



  • 9.  RE: VM Snapshot

    Posted Nov 02, 2017 10:15 PM

    What will be best solution if 5 people want to excute  The script at same time on different server ?



  • 10.  RE: VM Snapshot

    Posted Nov 03, 2017 05:36 AM

    The Get-Snapshot will not return anything, so there will be no object reaching the Remove-Snapshot cmdlet.

    Should 2 scripts reach the same snapshot at the same time, the Remove-Snapshot will probably give an error.

    You can try to add an -ErrorAction SilentlyContinue on the Remove-Snapshot cmdlet.



  • 11.  RE: VM Snapshot

    Posted Nov 01, 2017 11:29 AM

    Hello,

    Is possible to use same script to delete snapshots?

    I trying but getting error, not sure if this is correct:

    $vmlist = Get-Content C:\Users\gemela\Desktop\snap.txt

    foreach($VM in $VMlist) {

        Remove-Snapshot -VM $vm -snapshot -confirm:$false

    }

    Disconnect-VIServer -Confirm:$false 



  • 12.  RE: VM Snapshot

    Posted Nov 01, 2017 12:01 PM

    You have to provide a Snapshot object to Remove-Snapshot, so you would have to do something like this

    $vmlist = Get-Content C:\Users\gemela\Desktop\snap.txt

    foreach($VM in $VMlist) {

        Get-Snapshot -VM $vm |

        Remove-Snapshot -Confirm:$false

    }

    Disconnect-VIServer -Confirm:$false 



  • 13.  RE: VM Snapshot

    Posted Nov 01, 2017 02:10 PM

    It works, Thanks a lot LucD



  • 14.  RE: VM Snapshot

    Posted Sep 01, 2020 09:22 PM
    Hi, What about removing just a specific snapshot that matches description (used previously)


  • 15.  RE: VM Snapshot

    Posted Sep 01, 2020 09:34 PM

    What about adding delays or that script would go in sequence with delay or that would go to next one only when previous one (or previous "bach" of say 5) is completed? Also as an option would be bit more "intelligent" if it would monitor if set number of snapshot tasks in progress it would not progress with next ones to not "hammer" the environment too mutch.

    Help appreciated. Thank you.



  • 16.  RE: VM Snapshot

    Posted Sep 02, 2020 06:59 AM

    To only select snapshots with a specific Description, you can use a Where-clause after the Get-Snapshot cmdlet.