Importing Scheduled Task with Encrypted Password

I am attempting to import a scheduled task that is in xml format. I need to run it as a different user. I would also like to encrypt the password for that user.

I encrypted the password doing the following:

$credential = Get-Credential

$credential.password | ConvertFrom-SecureString | Out-File C:\temp\password.txt

I then tried the following to import:

$username = “domain\user”

$SecurePassword = Get-Content -Path .\password.txt | ConvertTo-SecureString

if($(Get-ScheduledTask -TaskName “TaskName” -ErrorAction SilentlyContine).TaskName - eq “TaskName”) {

Exit

}

Else {

Register-ScheduledTask - Xml (Get-Content ‘.\TaskName.xml’ | out-string) -TaskName “TaskName” -User $username -Password $SecurePassword

}

When I test the above import script in the ISE, I get the following error for the Register-ScheduledTask line:

Register-ScheduledTask : The user name or password is incorrect

Now, I replace -Password with the actual password and the task is imported.

I ran Get-Credential above to get the encrypted password with the system account.

I am running the script to import/register the task with the same system account.

Can someone point me to why I’m getting the incorrect username or password error?

Also, I will be deploying the import script using SCCM. So, the script will be run on different machines (that is why I’m using the system account to capture the credentials as that will be the account to run the import script. Will that be ok?

The SecureString can only be decrypted under the same user context that encrypted it in the first place.

@sam boutros - I encrypted it using powershell as the system account and I’m running the the script for testing (and deploying) as the system account. Is that correct?