VMware Cloud Community
Halukkocaman
Enthusiast
Enthusiast

Schedule Snapshot with PowerCli

Hi All,

I there any way to use PowerCli to schedule a snapshot? I'm looking to built a script to take user inputs such as Vm name, snapshot name, snapshot date, etc. to schedule a snapshot. I'm hoping to create a simple webpage to take some user inputs and pass it to script. I saw some examples that use PowerShell to create schedule task but they are all about creating a windows scheduled task. I would like to create the task as you would create in vSphere.

Thanks,

Haluk

Tags (2)
57 Replies
hpkishore
Contributor
Contributor

Hello All,

 

Im new VMware ,and im trying to create a script to take the snapshot for multiple vm`s ( 2000+) for an upgrade for a particular time, but the scripts mentioned in this thread is not working. i have been using the below script ,but its not throwing any error or snapshot is scheduled. So can i have the script to schedule a snapshots? or advise me if im missing anything

 

$snapTime = Get-Date "05/05/24 11:00"
$snapName = 'Test'
$snapDescription = 'Test Scheduled snapshot'
$snapMemory = $true
$snapQuiesce = $false
#$emailAddr = 'xxx@xxx.com'
$fileName = 'C:\Users\servers.csv'
$vcName = 'MITR004'
######################################################################
Import-Csv -Path $fileName -UseCulture | %{

$vm = Get-VM -Name $_.VMName
$si = get-view ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Snapshot",$vm.Name -join ' '
$spec.Description = $_.Description
$spec.Enabled = $true
$spec.Notification = $emailAddr

$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $snapTime

$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "CreateSnapshot_Task"

@($snapName,$snapDescription,$snapMemory,$snapQuiesce) | %{
$arg = New-Object VMware.Vim.MethodActionArgument
$arg.Value = $_
$spec.Action.Argument += $arg
}

$scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)
}

0 Kudos
LucD
Leadership
Leadership

That script works perfectly for me.


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

0 Kudos
hpkishore
Contributor
Contributor

Are you running using powershell or any other method ? im running powershell from my laptop .

can you please advise how to run it?

0 Kudos
LucD
Leadership
Leadership

You can run it from any PS session, provided you have the PowerCLI modules installed.
How do you run it?
Perhaps a screenshot might help.


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

0 Kudos
hpkishore
Contributor
Contributor

I use the above script

 

 

and its just connecting to the VC but its not taking any snapshot. and servers.csv just contains the vm name without any heading

 

 

 

0 Kudos
LucD
Leadership
Leadership

The CSV file should have a column name of VMName, and the name of a VM on the next line

VMName
testvm1

 


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

0 Kudos
hpkishore
Contributor
Contributor

Im getting the below error

 

 

 

 

 

0 Kudos
LucD
Leadership
Leadership

Your Connect-VIServer is not working, check the $vcName variable


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

0 Kudos
hpkishore
Contributor
Contributor

Im running with the VC name ,but getting the below error

 

 

 

0 Kudos
LucD
Leadership
Leadership

It looks as if there is no VM with the name you entered in the CSV on that vCenter.
What is in the CSV file?
Does a Get-VM return anything when using that name?


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

0 Kudos
hpkishore
Contributor
Contributor

servers,csv has 2 vm`s with VMName as header

0 Kudos
LucD
Leadership
Leadership

Is there perhaps a blank line in the CSV, at the end?


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

0 Kudos
hpkishore
Contributor
Contributor

I just validated , all the files and its lines looks good 

0 Kudos
LucD
Leadership
Leadership

Then I'm afraid I can't help you any further


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

0 Kudos
hpkishore
Contributor
Contributor

Hi

 

Im getting this error , i see the snapshot has been schedule but  i have to run for a single VM at at a time, and then 2nd vm im getting the below error

 

hpkishore_0-1712774702013.png

 

 

 

0 Kudos
LucD
Leadership
Leadership

I think the message is clear, that VM is in a funny state.


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

0 Kudos
hpkishore
Contributor
Contributor

any recommendations to fix ?

0 Kudos
LucD
Leadership
Leadership

Test the creation of the Scheduled Tasks with other VMs.

No clue how you can fix those VMs that are in a funny state


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

0 Kudos