Creating a Scheduled Task by Using PSADT on an SCCM Enviroment

Hi Guys,

im trying to create a scheduled Task for the currently Logged On User

		##Create task for a backup of the Notes on drive Y:\
				
		# Defining the task parameters
		$taskName = "StickyNotesBackup"
		$TaskDescription = "Backup of the Sticky Notes database daily at 4 a.m."
		$actionPath = "robocopy.exe"
		$actionArguments = "`"C:\StickyNotesDB`" `"Y:\Simple Sticky Notes\Backup`" Notes.db /XO /W:5 /R:2"
		$hidden = $true

		# Check whether the task planning module is loaded and load it if necessary
		if (-not (Get-Module -ListAvailable -Name TaskScheduler)) {
		Import-Module TaskScheduler
		}

		# Create the triggers for daily execution at 4 a.m.
		$triggerDaily = New-ScheduledTaskTrigger -Daily -At "4:00am"

		# Creating the task action
		$action = New-ScheduledTaskAction -Execute $actionPath -Argument $actionArguments

		# Configuring the task for the current user
		$taskUser = "$env:USERDOMAIN\$env:USERNAME"
		$taskPrincipal = New-ScheduledTaskPrincipal -UserId $taskUser -LogonType S4U
		$taskSettings = New-ScheduledTaskSettingsSet -Hidden -RunOnlyIfIdle:$false -DontStopIfGoingOnBatteries:$false -StartWhenAvailable:$true -DontStopOnIdleEnd:$false

		# Creating the task
		Register-ScheduledTask -TaskName $taskName -Description $TaskDescription -Action $action -Trigger $triggerDaily -Principal $taskPrincipal -Settings $taskSettings

If i run this Part of my Deployment Script locally (run Deploy-Application.exe) it works fine, if i run it by using SCCM the task is not created. i´m pretty sure its because SCCM Installation uses the SYSTEM Account to perform its Actions.

Would be anyone of you that nice to help me to run that script using SCCM?

Try setting $taskUser to the local USERS group.

The the task will fire for all logged on users.

unfortunately that doesn’t work either. i think you mean it that way:

		# Defining the task parameters
		$taskName = "StickyNotesBackup"
		$TaskDescription = "Backup der Sticky Notes Datanbank täglich um 4 Uhr morgens"
		$actionPath = "robocopy.exe"
		$actionArguments = "`"C:\StickyNotesDB`" `"Y:\Simple Sticky Notes\Backup`" Notes.db /XO /W:5 /R:2"
		$hidden = $true

		# Check whether the task planning module is loaded and load it if necessary
		if (-not (Get-Module -ListAvailable -Name TaskScheduler)) {
		Import-Module TaskScheduler
		}

		# Create the triggers for daily execution at 4 a.m.
		$triggerDaily = New-ScheduledTaskTrigger -Daily -At "4:00am"

		# Creating the task action
		$action = New-ScheduledTaskAction -Execute $actionPath -Argument $actionArguments

		# Configuring the task for current user group
		$taskUser = "BUILTIN\Users"
		$taskPrincipal = New-ScheduledTaskPrincipal -UserId $taskUser -LogonType S4U
		$taskSettings = New-ScheduledTaskSettingsSet -Hidden -RunOnlyIfIdle:$false -DontStopIfGoingOnBatteries:$false -StartWhenAvailable:$true -DontStopOnIdleEnd:$false

		# Creating the task
		Register-ScheduledTask -TaskName $taskName -Description $TaskDescription -Action $action -Trigger $triggerDaily -Principal $taskPrincipal -Settings $taskSettings

With this code it is neither created under the system account, nor when I run it with local admin rights.

I just can’t find the error in my thinking

Solution for me:

Import the Task with an XML File.

It’s not nearly as much of a headache as my attempt

		Write-Log "Create Scheduled Task using built-in Win10 commands..."
		[String]$TaskName =		"Sticky Notes Backup"
		[String]$TaskXMLPath = "$dirSupportFiles\StickyNotesBackup.xml"
		[String]$TaskXmlFileContent = Get-Content $TaskXMLPath
		$Error.Clear()
		$Results = Register-ScheduledTask -TaskName $TaskName -Xml $TaskXmlFileContent -Force -ErrorAction Continue 2>&1 | Out-String
		Write-log "[$Results]"
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.