Automation

 View Only
Expand all | Collapse all

create snapshot for bunch of VMs with unique name of each

  • 1.  create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 01:05 AM

    Hello team

    I'm trying to create a script to take snapshot for bunch of VMs at one shoot and each snapshot should has a unique snap name (prefers Snapshot name format : 'VM_Name - time'

    Script:

    $importVMfile = 'VM1', 'VM2', 'VM3', 'VM4'
    $TimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    Get-VM | Where-Object {$_.name -in $importVMfile} | New-Snapshot -name "$_.Name - $TimeforSnap" -Confirm:$false -RunAsync

    script output:

    VMSnap-Name
    VM1$_.Name - 20220910 20:55:42
    VM2$_.Name - 20220910 20:55:42
    VM3$_.Name - 20220910 20:55:42
    VM4$_.Name - 20220910 20:55:42

     

    Expected output:

    VMSnap-Name
    VM1VM1 - 20220910 20:55:42
    VM2VM2- 20220910 20:55:42
    VM3VM3 - 20220910 20:55:42
    VM4VM4 - 20220910 20:55:42

     

    so how to get the expected output to allow its own name appear in the snap name

     

     

     

     



  • 2.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 06:56 AM

    Try like this

    $importVMfile = 'VM1', 'VM2', 'VM3', 'VM4'
    $TimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    Get-VM | Where-Object {$_.name -in $importVMfile} | New-Snapshot -name "$($_.Name) - $TimeforSnap" -Confirm:$false -RunAsync
    
    


  • 3.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 01:10 PM

      thank you

     

    I tried, and it doesn't show the VM name

    Script :

    $VMFilePath = get-content "C:\temp\vms.txt"
    $CreatedTimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    Get-VM | Where-Object {$_.name -in $VMFilePath} | New-Snapshot -name "$($_.Name) - $CreatedTimeforSnap" -Confirm:$false -RunAsync

    output:

    VM                      Snap-Name                                   Created-Time      

      Description

     Snap-PowerState

    CAS-WS1           - 20220913 08:52:42                   9/13/2022 8:52:43 AM

     

    PoweredOff



  • 4.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 01:24 PM

     

    I modified script  by setting get-vm to a variable($VMs) and then replace $_.Name with $VMs.name :

    But it takes all VMs name from the importFile rather to taking its own name

    Script:

    $VMFilePath = get-content "C:\temp\vms.txt"
    $CreatedTimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    $VMs = Get-VM | Where-Object {$_.name -in $VMFilePath}

    $VMs | New-Snapshot -name "$($VMs.name) - $CreatedTimeforSnap " -Confirm:$false -RunAsync

     

    VM      

    Snap-Name                                            Created-Time        

    Description Snap-PowerState

    CAS-WS1

    20220913 09:17:36 - CAS-WS1 CAS-CV-VMwareVSA    9/13/2022 9:16:57 AM 

     PoweredOff

     

     

     

     



  • 5.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 04:23 PM

    Try with

    $VMFilePath = get-content "C:\temp\vms.txt"
    $CreatedTimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    $VMs = Get-VM | Where-Object {$_.name -in $VMFilePath}
    
    $VMs | New-Snapshot -name "$($_.name) - $CreatedTimeforSnap " -Confirm:$false -RunAsync


  • 6.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 05:00 PM

      thanks

    tried and the VM name still missing 

    VM      

    Snap-Name                                            Created-Time        

    Description Snap-PowerState

    CAS-WS1

    - 20220913 12:55:2         9/13/2022 12:55:20 PM 

     PoweredOff

     



  • 7.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 05:17 PM

    Seems to work for me.

    What is in $VMs?



  • 8.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 05:43 PM

     

    $VMFilePath = get-content "C:\temp\vms.txt"
    $CreatedTimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    $VMs = Get-VM | Where-Object {$_.name -in $VMFilePath}

    $VMs | New-Snapshot -name "$($_.name) - $CreatedTimeforSnap " -Confirm:$false -RunAsync

    Golden275_7-1663092167474.png

     

    Golden275_5-1663091678473.png

     

     



  • 9.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 05:52 PM

    No, what I mean is what is in the variable $VMs after you run these lines.

    $VMFilePath = get-content "C:\temp\vms.txt"
    $CreatedTimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    $VMs = Get-VM | Where-Object {$_.name -in $VMFilePath}

     



  • 10.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 06:08 PM

    Golden275_0-1663092420808.png

     

    variable in $VMs



  • 11.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 06:18 PM

    Ok, now run this

    $VMs | %{"$($_.Name) - $CreatedTimeforSnap"}


  • 12.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 06:36 PM

     

    output:

    $VMFilePath = get-content "C:\temp\vms.txt"
    $CreatedTimeforSnap= get-date -format "yyyyMMdd HH:mm:ss"
    $VMs = Get-VM | Where-Object {$_.name -in $VMFilePath}

    $VMs | %{"$($_.Name) - $CreatedTimeforSnap"}

     

    Golden275_0-1663094152104.png

     



  • 13.  RE: create snapshot for bunch of VMs with unique name of each
    Best Answer

    Posted Sep 13, 2022 06:54 PM

    Ok, then try this way

    $VMFilePath = Get-Content "C:\temp\vms.txt"
    $CreatedTimeforSnap = Get-Date -Format "yyyyMMdd HH:mm:ss"
    $VMs = Get-VM | Where-Object { $_.name -in $VMFilePath }
    
    $VMs | ForEach-Object -Process {
       New-Snapshot -VM $_ -Name "$($_.Name) - $CreatedTimeforSnap " -Confirm:$false -RunAsync
    }


  • 14.  RE: create snapshot for bunch of VMs with unique name of each

    Posted Sep 13, 2022 08:16 PM

     
    thank you very much and it works 

    Golden275_2-1663100179872.png