Skip to content

Instantly share code, notes, and snippets.

@csandfeld
Last active October 30, 2015 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csandfeld/08d4242b5218cc4f9934 to your computer and use it in GitHub Desktop.
Save csandfeld/08d4242b5218cc4f9934 to your computer and use it in GitHub Desktop.
Image Factory: Create Scheduled Tasks
Param(
[pscredential]
$Credential
,
[String]
$TaskPath = ''
)
$TaskName = 'ImageFactory - Build {0} images'
# Prompt for credential if none was passed
if (-not $Credential) {
$Credential = Get-Credential -Message 'Image Factory Service Account:'
}
$BuildTasks = @(
'All',
'Hybrid',
'Thin'
)
Write-Output 'Creating Scheduled Task:'
foreach ($Task in $BuildTasks) {
if ($TaskPath -ne '') {
"$TaskPath\$TaskName" -f $Task
}
else {
$TaskName -f $Task
}
#region Task Actions
$Parameters = @{
'Execute' = 'powershell.exe'
'Argument' = "-NoProfile -ExecutionPolicy Bypass -File .\Build.ps1 -BuildTask $Task"
'WorkingDirectory' = $PSScriptRoot
}
$Action = New-ScheduledTaskAction @Parameters
#endregion
#region Task Settings Set
$Parameters = @{
}
$Setting = New-ScheduledTaskSettingsSet @Parameters
#endregion
#region New Task
$Parameters = @{
'Action' = $Action
'Settings' = $Setting
}
$ScheduledTask = New-ScheduledTask @Parameters
#endregion
#region Register Task
$Parameters = @{
'InputObject' = $ScheduledTask
'TaskName' = $TaskName -f $Task
'User' = $Credential.UserName
'Password' = $Credential.GetNetworkCredential().Password
}
if ($TaskPath -ne '') { $Parameters['TaskPath'] = $TaskPath }
[void](Register-ScheduledTask @Parameters)
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment