Last active
October 30, 2015 10:42
-
-
Save csandfeld/08d4242b5218cc4f9934 to your computer and use it in GitHub Desktop.
Image Factory: Create Scheduled Tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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