Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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