Register scheduled task works perfectly from workstation

0

This script works great from my workstation during testing. It grabs the username disable date time and enable date and time from a CSV then creates a scheduled task to expire the account during a specific lockout time. It will then unlock the account during the enable process. I have since installed on the server Windows 2012 R2 and now I receive an error.

I have run Update-Module -force and also tried PowerShell 7

# Set RUN AS user and Password
if($credentials -eq $null){
    $credentials = Get-Credential 
}



# Set Args
$scriptDefaultArgs = '-executionpolicy bypass'
$csvFilePath = "C:\SCHEDULED_TASKS\VacationUsers\vacation.csv"

# static Scheduled Task settings
$STSettings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -StartWhenAvailable 
$STSettings.DeleteExpiredTaskAfter = "PT0S"
$STPrinciple = New-ScheduledTaskPrincipal -LogonType S4U -RunLevel Highest -UserId $credentials.UserName 

# Import CSV 
$accounts = Import-Csv $csvFilePath 





foreach ($account in $accounts){
    if($account.entered -eq "completed" -or !$account ){continue}
    if((get-date $account.DisableDate) -lt (get-date)){$account.DisableDate = (get-date).AddMinutes(1).ToString("MM/dd/yyyy HH:mm:ss")}
    if((get-date $account.EnableDate) -lt (get-date)){$account.EnableDate = (get-date).AddMinutes(2).ToString("MM/dd/yyyy HH:mm:ss")}
    # Created Scheduled Task #
   
    # Enable User Task
    $STactionEnableUser = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "$scriptDefaultArgs -command `"clear-adaccountexpiration -identity $($account.user)`"" 
    $STEnableTrigger = New-ScheduledTaskTrigger  -Once -At $account.enableDate  
    $STEnableTrigger.EndBoundary = (get-date $account.EnableDate).AddMinutes(1).ToString("yyyy-MM-dd'T'HH:mm:ss")
    $STDef = New-ScheduledTask  -Action $STactionEnableUser -Trigger $STEnabletrigger -Settings $STSettings -Description "enable $($account.user)"
    $task = Register-ScheduledTask -TaskName "Enable $($account.user)" -InputObject $STDef -User $credentials.UserName -Password $credentials.GetNetworkCredential().Password
    
    
    $STactionDisableUser = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "$scriptDefaultArgs -command set-adaccountexpiration -identity $($account.user) -datetime $($account.disableDate)`""
    $STDisableTrigger = New-ScheduledTaskTrigger -Once -At $account.disableDate
    $STDisableTrigger.EndBoundary = (get-date $account.DisableDate).AddMinutes(1).ToString("yyyy-MM-dd'T'HH:mm:ss") 
    $STDef = New-ScheduledTask  -Action $STactionDisableUser -Trigger $STDisabletrigger -Settings $STSettings -Description "disable $($account.user)"
    $task = Register-ScheduledTask -TaskName "Disable $($account.user)" -InputObject $STDef -User $credentials.UserName -Password $credentials.GetNetworkCredential().Password

    $account.entered = "Completed"
    
}

$accounts | Export-Csv -Path $csvFilePath -Force -NoTypeInformation

ERROR

The property 'EndBoundary' cannot be found on this object. Verify that the property exists and can be set.
At line:33 char:5
+     $STEnableTrigger.EndBoundary = (get-date $account.EnableDate).Add ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Register-ScheduledTask : The task XML is missing a required element or attribute.
(43,4):EndBoundary:
At line:35 char:13
+     $task = Register-ScheduledTask -TaskName "Enable $($account.user) ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register 
   -ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041319,Register-ScheduledTask
 
The property 'EndBoundary' cannot be found on this object. Verify that the property exists and can be set.
At line:40 char:5
+     $STDisableTrigger.EndBoundary = (get-date $account.DisableDate).A ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Register-ScheduledTask : The task XML is missing a required element or attribute.
(43,4):EndBoundary:
At line:42 char:13
+     $task = Register-ScheduledTask -TaskName "Disable $($account.user ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register 
   -ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041319,Register-ScheduledTask
 
powershell
powershell-5.0
asked on Stack Overflow Sep 25, 2020 by user99987 • edited Sep 25, 2020 by Mathias R. Jessen

1 Answer

0

Personally I've never really liked the PowerShell cmdlets for scheduled task manipulation and prefer to use the ComObject for the scheduler. Some of it is a little harder to follow, but looking at method overloads and using the Get-Member cmdlet to find object types, then searching the internet for those types to find documentation usually takes care of questions that arise as I work with things. Here's an example for what you're doing:

$Scheduler = New-Object -ComObject Schedule.Service
$Scheduler.Connect()
$RootFolder = $Scheduler.GetFolder("\")

#Define Enable User Task (general settings, user specific settings happen in loop)
$Task = $Scheduler.NewTask(0)
$Task.RegistrationInfo.Author = [Security.Principal.WindowsIdentity]::GetCurrent().Name
$Task.Settings.Enabled = $true
$Task.Settings.Hidden = $false
$Task.Settings.RunOnlyIfNetworkAvailable = $true
$Task.Settings.StartWhenAvailable = $true
$Task.Settings.DeleteExpiredTaskAfter = 'PT0S'
$Task.Principal.RunLevel = 1
$Action = $Task.Actions.Create(0)
$Action.Path = 'powershell.exe'
$Trigger = $Task.Triggers.Create(1)

foreach ($account in $accounts){
    if($account.entered -eq "completed" -or !$account ){continue}
    if((get-date $account.DisableDate) -lt (get-date)){$account.DisableDate = (get-date).AddMinutes(1).ToString("MM/dd/yyyy HH:mm:ss")}
    if((get-date $account.EnableDate) -lt (get-date)){$account.EnableDate = (get-date).AddMinutes(2).ToString("MM/dd/yyyy HH:mm:ss")}
    # Created Scheduled Task #
   
    # Enable User Task
    $Task.RegistrationInfo.Description = "enable $($account.user)"
    $Action.Arguments = "$scriptDefaultArgs -command `"clear-adaccountexpiration -identity $($account.user)`""
    $Trigger.StartBoundary = $account.enableDate  
    $Trigger.EndBoundary = (get-date $account.EnableDate).AddMinutes(1).ToString("yyyy-MM-dd'T'HH:mm:ss")
    $RootFolder.RegisterTaskDefinition("Enable $($account.user)",$Task,6,$credentials.UserName,$credentials.GetNetworkCredential().Password,1)

    
    $Task.RegistrationInfo.Description = "Disable $($account.user)"
    $Action.Arguments = "$scriptDefaultArgs -command set-adaccountexpiration -identity $($account.user) -datetime $($account.disableDate)`""
    $Trigger.StartBoundary = $account.disableDate
    $Trigger.EndBoundary = (get-date $account.DisableDate).AddMinutes(1).ToString("yyyy-MM-dd'T'HH:mm:ss") 
    $RootFolder.RegisterTaskDefinition("Disable $($account.user)",$Task,6,$credentials.UserName,$credentials.GetNetworkCredential().Password,1)

    $account.entered = "Completed"
    
}
answered on Stack Overflow Sep 25, 2020 by TheMadTechnician

User contributions licensed under CC BY-SA 3.0