Importing Scheduled Task with Encrypted Password

Welcome Forums General PowerShell Q&A Importing Scheduled Task with Encrypted Password

Viewing 2 reply threads
  • Author
    Posts
    • #231361
      Participant
      Topics: 4
      Replies: 9
      Points: 14
      Rank: Member

      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?

    • #231451
      Participant
      Topics: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero

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

    • #231550
      Participant
      Topics: 4
      Replies: 9
      Points: 14
      Rank: Member

      @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?

Viewing 2 reply threads
  • You must be logged in to reply to this topic.