Use helper methods to validate vars. Cleanup logic.

This commit is contained in:
Michael Perzel 2015-09-08 11:18:26 -05:00 committed by Matt Clay
parent dae8cfe0a9
commit 0d78c01d32

View file

@ -26,22 +26,29 @@ $params = Parse-Args $args;
$result = New-Object PSObject; $result = New-Object PSObject;
Set-Attr $result "changed" $false; Set-Attr $result "changed" $false;
if ($params.name) #Required vars
{ $name = Get-Attr -obj $params -name name -failifempty $true -resultobj $result
$name = $params.name $state = Get-Attr -obj $params -name state -failifempty $true -resultobj $result
if( ($state -ne "present") -and ($state -ne "absent") ) {
Fail-Json $result "state must be present or absent"
} }
else
{ #Vars conditionally required
Fail-Json $result "missing required argument: name" if($state -eq "present") {
$execute = Get-Attr -obj $params -name execute -failifempty $true -resultobj $result
$frequency = Get-Attr -obj $params -name frequency -failifempty $true -resultobj $result
$time = Get-Attr -obj $params -name time -failifempty $true -resultobj $result
} }
if ($params.state) if ($params.daysOfWeek)
{ {
$state = $params.state $daysOfWeek = $params.daysOfWeek
} }
else elseif ($frequency -eq "weekly")
{ {
Fail-Json $result "missing required argument: state" Fail-Json $result "missing required argument: daysOfWeek"
} }
# Vars with defaults
if ($params.enabled) if ($params.enabled)
{ {
$enabled = $params.enabled | ConvertTo-Bool $enabled = $params.enabled | ConvertTo-Bool
@ -58,17 +65,6 @@ else
{ {
$description = " " #default $description = " " #default
} }
if ($params.execute)
{
$execute = $params.execute
}
elseif ($state -eq "present")
{
Fail-Json $result "missing required argument: execute"
}
if( $state -ne "present" -and $state -ne "absent") {
Fail-Json $result "state must be present or absent"
}
if ($params.path) if ($params.path)
{ {
$path = "\{0}\" -f $params.path $path = "\{0}\" -f $params.path
@ -77,30 +73,6 @@ else
{ {
$path = "\" #default $path = "\" #default
} }
if ($params.frequency)
{
$frequency = $params.frequency
}
elseif($state -eq "present")
{
Fail-Json $result "missing required argument: frequency"
}
if ($params.time)
{
$time = $params.time
}
elseif($state -eq "present")
{
Fail-Json $result "missing required argument: time"
}
if ($params.daysOfWeek)
{
$daysOfWeek = $params.daysOfWeek
}
elseif ($frequency -eq "weekly")
{
Fail-Json $result "missing required argument: daysOfWeek"
}
try { try {
$task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"} $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"}