{"id":1218,"date":"2013-03-05T09:00:02","date_gmt":"2013-03-05T14:00:02","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=1218"},"modified":"2013-03-03T09:32:42","modified_gmt":"2013-03-03T14:32:42","slug":"new-vcenter-scheduled-tasks-with-powercli-part-2","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/new-vcenter-scheduled-tasks-with-powercli-part-2\/","title":{"rendered":"New vCenter Scheduled Tasks with PowerCLI (Part 2)"},"content":{"rendered":"<p>In a recent post <a href=\"http:\/\/enterpriseadmins.org\/blog\/scripting\/get-vcenter-scheduled-tasks-with-powercli-part-1\">New vCenter Scheduled Tasks with PowerCLI (Part 1)<\/a>, I provided two functions that return information related to vCenter Scheduled Tasks.  This was my first step towards creating a PowerShell function that I could run to create scheduled snapshot tasks.  This post will cover the create scheduled snapshot function.<\/p>\n<p>NOTE: This function requires two additional functions, <strong>Get-VMScheduledSnapshots<\/strong> and <strong>Get-VIScheduledTasks<\/strong>, available here: <a href=\"http:\/\/enterpriseadmins.org\/blog\/scripting\/get-vcenter-scheduled-tasks-with-powercli-part-1\">New vCenter Scheduled Tasks with PowerCLI (Part 1)<\/a>.<\/p>\n<pre><code class=\"language-powershell\">\r\nFunction New-VMScheduledSnapshot {\r\nPARAM (\r\n  [string]$vmName,\r\n  [string]$runTime,\r\n  [string]$notifyEmail=$null,\r\n  [string]$taskName=\"$vmName Scheduled Snapshot\"\r\n)\r\n\r\n# Verify we found a single VM\r\n$vm = (get-view -viewtype virtualmachine -property Name -Filter @{\"Name\"=\"^$($vmName)$\"}).MoRef\r\nif (($vm | Measure-Object).Count -ne 1 ) { \"Unable to locate a specific VM $vmName\"; break }\r\n\r\n# Validate datetime value and convert to UTC\r\ntry { $castRunTime = ([datetime]$runTime).ToUniversalTime() } catch { \"Unable to convert runtime parameter to date time value\"; break }\r\nif ( [datetime]$runTime -lt (Get-Date) ) { \"Single run tasks can not be scheduled to run in the past.  Please adjust start time and try again.\"; break }\r\n\r\n# Verify the scheduled task name is not already in use\r\nif ( (Get-VIScheduledTasks | ?{$_.Name -eq $taskName } | Measure-Object).Count -eq 1 ) { \"Task Name `\"$taskName`\" already exists.  Please try again and specify the taskname parameter\"; break }\r\n\r\n$spec = New-Object VMware.Vim.ScheduledTaskSpec\r\n$spec.name = $taskName\r\n$spec.description = \"Snapshot of $vmName scheduled for $runTime\"\r\n$spec.enabled = $true\r\nif ( $notifyEmail ) {$spec.notification = $notifyEmail}\r\n($spec.scheduler = New-Object VMware.Vim.OnceTaskScheduler).runAt = $castRunTime\r\n($spec.action = New-Object VMware.Vim.MethodAction).Name = \"CreateSnapshot_Task\"\r\n$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4)\r\n($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = \"$vmName scheduled snapshot\"\r\n($spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument).Value = \"Snapshot created using $taskName\"\r\n($spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument).Value = $false # Snapshot memory\r\n($spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument).Value = $false # quiesce guest file system (requires VMware Tools)\r\n\r\n[Void](Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager').CreateScheduledTask($vm, $spec)\r\nGet-VMScheduledSnapshots | ?{$_.Name -eq $taskName }\r\n}\r\n<\/code><\/pre>\n<pre>\r\n# Create a snapshot of the VM test002 at 9:40AM on 3\/2\/13\r\nNew-VMScheduledSnapshot test002 \"3\/2\/13 9:40AM\"\r\n\r\n# Create a snapshot and send an email notification\r\nNew-VMScheduledSnapshot test002 \"3\/2\/13 9:40AM\" myemail@mydomain.com\r\n\r\n# Use all of the options and name the parameters\r\nNew-VMScheduledSnapshot -vmname 'test001' -runtime '3\/2\/13 9:40am' -notifyemail 'myemail@mydomain.com' -taskname 'My scheduled task of test001'\r\n<\/pre>\n<p>I hope someone finds this function useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent post New vCenter Scheduled Tasks with PowerCLI (Part 1), I provided two functions that return information related to vCenter Scheduled Tasks. This was my first step towards creating a PowerShell function that I could run to create &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/new-vcenter-scheduled-tasks-with-powercli-part-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-1218","post","type-post","status-publish","format-standard","hentry","category-scripting","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1218"}],"version-history":[{"count":2,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1218\/revisions"}],"predecessor-version":[{"id":1221,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1218\/revisions\/1221"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}