VMware Cloud Community
khalilm
Contributor
Contributor

Help with Scheduled Snapshot Task using CSV

Hi

I am trying to setup a scheduled task orginally taken from (https://communities.vmware.com/thread/541573), we running ESXi 6.7 U3, the task is being created but the date/time is not being passed through. Any help appreciated

The CSV format is

VMName, Description, Date, Time

The script is

# CSV File Format

# VMName,Description

# VM1,Test1

##########################

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'xxx@xxx.com

$fileName = 'C:\Temp\Scripts\snapshot.csv'

###############

Import-Csv -Path $fileName -UseCulture | %{

# Verify the scheduled task name is not already in use

    $vm = Get-VM -Name $_.VMName

    $si = get-view ServiceInstance

    $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

    $spec = New-Object VMware.Vim.ScheduledTaskSpec

    $spec.Name = "Snapshot of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

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

    $spec.Scheduler.runat = $_.Date," ",$_.Time -join ''

    $spec.Action = New-Object VMware.Vim.MethodAction

    $spec.Action.Name = "CreateSnapshot_Task"

    @($spec.Name,$spec.Description,$snapMemory,$snapQuiesce) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

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

}

20 Replies
khalilm
Contributor
Contributor

Sorry forgot to add that i am new to powercli, i have very basic understanding

thank you in advance

0 Kudos
LucD
Leadership
Leadership

The script is using the properties Date and Time, but the description of the CSV doesn't mention those columns.

What do you have in the CSV?


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

0 Kudos
khalil1974
Enthusiast
Enthusiast

hi, thank you for you reply, here is screenshot of the csv

pastedImage_0.png

Here screenshot of the error

pastedImage_1.png

0 Kudos
khalil1974
Enthusiast
Enthusiast

Hi, sorry i know what the error for the 2nd message (the task is already created), it is just the date and time issue pls

0 Kudos
LucD
Leadership
Leadership

Switch the day and the month


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

khalil1974
Enthusiast
Enthusiast

HI, thank you i tried that but i get a different error

CSV file is now

pastedImage_0.png

pastedImage_1.png

It seems that the date includes the addtional time maybe i should merge both datetime into one

0 Kudos
LucD
Leadership
Leadership

Why is the time twice in that string?


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

khalil1974
Enthusiast
Enthusiast

Thank you sir, i expand the column and had the time twice work great

pastedImage_0.png

0 Kudos
khalil1974
Enthusiast
Enthusiast

Hi

thank you for you help, pls can i ask another question, if i wanted to have the script to have the scheduled changed to weekly i assume i see need to look the following

CSV file will be something like

Script will need to change to

use object "New-Object VMware.Vim.WeeklyTaskScheduler"

$spec.Scheduler.$_.On = $true

$spec.Scheduler.Hour = $_.StartTimeHour

$spec.Scheduler.Minute = $_.StartTimeMin

$spec.Scheduler.Interval = $_.On

0 Kudos
LucD
Leadership
Leadership

Try something like this

$obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

$obj.Hour = $_.StartTimeHour

$obj.Minute = $_.StartTimeMin

$obj."$($_.On)" = $true

$obj.Interval = $_.Repeat

$obj.ActiveTime = $_.StartDate


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

khalil1974
Enthusiast
Enthusiast

Hi

Thank you again for you help, here is the script but i am still seeing error,(see it the last line) pls can you help and guide me

##########################

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'xxx@xxx.com'

$fileName = 'C:\Temp\Scripts\VMware PowerCLI\snapshot-weekly.csv'

###############

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 of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

    $obj.Hour = $_.StartTimeHour

    $obj.Minute = $_.StartTimeMin

    $obj."$($_.On)" = $true

    $obj.Interval = $_.Repeat

    $obj.ActiveTime = $_.StartDate

    $spec.Action = New-Object VMware.Vim.MethodAction

    $spec.Action.Name = "CreateSnapshot_Task"

    @($spec.Name,$spec.Description,$snapMemory,$snapQuiesce,$emailAddr,$obj.Hour,$obj.Minute,$obj."$($_.On)",$obj.Interval,$obj.ActiveTime) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

    $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec, $obj)

}

error is

pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership

You are using a different method.
CreateScheduledTask and CreateObjectScheduledTask are two different methods.
I don't know why you changed the method.

Independent, each of the methods uses 2 parameters, while you have 3.
The TaskScheduler object goes in the Scheduler property of the $spec.

##########################

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'xxx@xxx.com'

$fileName = 'C:\Temp\Scripts\VMware PowerCLI\snapshot-weekly.csv'

###############

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 of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

    $obj.Hour = $_.StartTimeHour

    $obj.Minute = $_.StartTimeMin

    $obj."$($_.On)" = $true

    $obj.Interval = $_.Repeat

    $obj.ActiveTime = $_.StartDate

    $spec.Scheduler = $obj

    $spec.Action = New-Object VMware.Vim.MethodAction

    $spec.Action.Name = "CreateSnapshot_Task"

    @($spec.Name,$spec.Description,$snapMemory,$snapQuiesce) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

    $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

}


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

khalil1974
Enthusiast
Enthusiast

Hi

Thank you for you help, i can see that you kindly edited the script from me to remove the $obj

$scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

I was looking a number of scripts and thought the CreateScheduledTask might be best option, thank you for pointing out my error.

I have tried to run the script again and still getting an error

pastedImage_2.png

I thought it might be because i have last says  vm.ExtensionData.MoRef and this should be $vm.MoRef but this gave error again.  Sorry have missed something

0 Kudos
LucD
Leadership
Leadership

I hadn't noticed that you changed the Action.Arguments as well.
I corrected the code above.


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

khalil1974
Enthusiast
Enthusiast

Thank you sir, works perfectly

0 Kudos
khalil1974
Enthusiast
Enthusiast

Hi can i ask i assumed that is i wanted to the setup a weekly reboot i can use the same script but thought the only think i would need to change is

$snapMemory = $false REMOVE

$snapQuiesce = $true REMOVE

Change the action to $spec.Action.Name = "RebootGuest"

remove unused variable from and this will be @($spec.Name,$spec.Description) , updated script below but it seem there is error again, can you pls point me in the right direction

##########################

$emailAddr = 'xxx@xxx.com'

$fileName = 'C:\Temp\Scripts\VMware PowerCLI\reboot-weekly.csv'

###############

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 = "Reboot of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

    $obj.Hour = $_.StartTimeHour

    $obj.Minute = $_.StartTimeMin

    $obj."$($_.On)" = $true

    $obj.Interval = $_.Repeat

    $obj.ActiveTime = $_.StartDate

    $spec.Scheduler = $obj

    $spec.Action = New-Object VMware.Vim.MethodAction

  $spec.Action.Name = "RebootGuest"

    @($spec.Name,$spec.Description) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

    $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

}

error

pastedImage_16.png

0 Kudos
LucD
Leadership
Leadership

Can you please open a new thread for this new question.
On condition that the question in this thread was answered of course.


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

0 Kudos
khalil1974
Enthusiast
Enthusiast

Hi

thank you i can confirm i happy the snapshot script is working

regarding other issue i will open a new thread

not sure how i can change this thread to answered

0 Kudos
LucD
Leadership
Leadership

I'm not sure how you are accessing this community, but via the VMTN web interface (for example Re: Help with Scheduled Snapshot Task using CSV ), there is a button named Correct Answer.

correct.jpg


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

0 Kudos