VMware Cloud Community
Dilwert
Contributor
Contributor

Scheduled Task for server snaps with email sent

I found this real nice script here and wanted to see if someone can help me modify it to do what I need.

I want to task schedule from a csv file to snap servers at a certain time and date.

I want an email sent out to requestor stating servers have been scheduled to snap. Also I want to check to see if there is a previous scheduled snap and deleted it if so.

I tried different methods but keep running into errors. Any help is much appriciated!

 

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

# These are the values you should get from your webform

#

$vmName = 'MyVM'

$snapTime = Get-Date "31/10/16 23:00"

$snapName = 'Test'

$snapDescription = 'Scheduled snapshot'

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'lucd@lucd.info'

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

 

$vm = Get-VM -Name $vmName

 

$si = get-view ServiceInstance

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

 

$spec = New-Object VMware.Vim.ScheduledTaskSpec

$spec.Name = "Snapshot",$_.VMname -join ' '

$spec.Description = "Take a snapshot of $($vm.Name)"

$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
25 Replies
LucD
Leadership
Leadership

Like this


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

0 Kudos
Dilwert
Contributor
Contributor

Thanks you . Much appreciated !!

0 Kudos
Dilwert
Contributor
Contributor

How can I get the script to pull the following fields from the cvs so I can keep the script from being modified?

from csv file:

server name

snap description

scheduled time to run

0 Kudos
LucD
Leadership
Leadership

Something like this?

$taskNameTemplate = 'Scheduled snapshot of $($vm.Name)'

$snapNameTemplate = 'Snapshot of $($vm.Name)'


$emailAddr = 'lucd@lucd.info'

$snapMemory = $false

$snapQuiesce = $true


$report = @()


$si = Get-View ServiceInstance

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


# CSV layout

# Name,Description,DateTime

# vm1,'Snapshot of VM1','06/15/2020 12:30'

# vm2,'Snapshot of VM2','06/16/2020 13:30'


Import-Csv -Path .\vmnames.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

# Get the target VM

    $vm = Get-VM -Name $row.Name


# Remove an existing scheduled task for the VM

    $scheduledTaskManager.RetrieveEntityScheduledTask($vm.ExtensionData.MoRef) |

    ForEach-Object -Process {

        Get-View -Id $_ | where{$_.Info.Name -match 'Snapshot of'} |

        ForEach-Object -Process {

            $_.RemoveScheduledTask()

        }

    }


# Create a new scheduled task

    $spec = New-Object VMware.Vim.ScheduledTaskSpec

    $spec.Name = $ExecutionContext.InvokeCommand.ExpandString($taskNameTemplate)

    $spec.Description = "Take a snapshot of $($vm.Name)"

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

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

    $spec.Scheduler.runat = [DateTime]$row.DateTime

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

    $spec.Action.Name = "CreateSnapshot_Task"

    $snapName = $ExecutionContext.InvokeCommand.ExpandString($snapNameTemplate)

    $snapDescription = $row.Description


    @($snapName,$row.Description,$snapMemory,$snapQuiesce) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

    $report += New-Object -TypeName PSObject -Property ([ordered]@{

        Server = $vm.Name

        Description = "Take a snapshot of $($vm.Name)"

        'Scheduled Date' = [DateTime]$row.DateTime

    })

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

}


$sMail = @{

    From = 'script@my.domain'

    To = 'me@my.domain'

    Subject = 'Scheduled Task Report'

    SmtpServer = 'mail.my.domain'

    BodyAsHtml = $true

    Body = $report | ConvertTo-Html | Out-String

}

Send-MailMessage @sMail


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

0 Kudos
Dilwert
Contributor
Contributor

I am getting the following error message:

Cannot convert value "" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

At line:73 char:5

+     $spec.Scheduler.runat = [DateTime]$row.DateTime

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

Cannot convert value "" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

At line:102 char:45

+         Description = "Take a snapshot of $($vm.Name)"

+                                             ~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

0 Kudos
LucD
Leadership
Leadership

You have to provide the timestamp in the CSV in a format that is accepted by your local settings.

To test the validity, you can do (insert your datetime string)

[DateTime]'05/31/2020 19:45'


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

0 Kudos