VMware Cloud Community
Golden275
Contributor
Contributor
Jump to solution

create snapshot for bunch of VMs with unique name of each

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

 

 

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

13 Replies
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Golden275
Contributor
Contributor
Jump to solution

@LucD  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

Reply
0 Kudos
Golden275
Contributor
Contributor
Jump to solution

@LucD 

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

 

 

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Golden275
Contributor
Contributor
Jump to solution

@LucD  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

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Seems to work for me.

What is in $VMs?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Golden275
Contributor
Contributor
Jump to solution

@LucD 

$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

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Golden275
Contributor
Contributor
Jump to solution

Golden275_0-1663092420808.png

@LucD 

variable in $VMs

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, now run this

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Golden275
Contributor
Contributor
Jump to solution

@LucD 

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

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Golden275
Contributor
Contributor
Jump to solution

@LucD 
thank you very much and it works 

Golden275_2-1663100179872.png

 

 

 

Reply
0 Kudos