2 of 2
Updated the answer per discussion in the comments
Adi Inbar
  • 268
  • 2
  • 8

Using PowerShell, you can store the username and encrypted password in a text file. The automated task can read the credentials credentials from the text file and create a PSCredential object, the use Start-Process to launch the executable using those credentials.

  1. Store the credentials:

     $path = <path_to_text_file>
     Read-Host "Username" | Out-File $path
     Read-Host "Password" -AsSecureString | ConvertFrom-SecureString | Out-File -Append $path
    

Note that ConvertFrom-SecureString does not decrypt the secure string into plain text; it converts it to cryptotext that can be stored in a file. After running this, you'll have a file with two lines. Line 1 contains the username, and line 2 contains the encrypted password.

  1. In the automated task, read the credentials and use them to launch the executable in that accounts context:

     $stored_creds = Get-Content <path_to_text_file>
     $username = $stored_creds[0]
     $password = $stored_creds[1] | ConvertTo-SecureString
     $credential = New-Object System.Management.Automation.PSCredential $username, $password
     Start-Process <path_to_executable> -Credential $credential 
    

Note that <path_to_executable> is just that: the path to the executable, not the full command line ('C:\Windows\System32\calc.exe' in your example). If you have any arguments, tack on -ArgumentList <the_rest_of_the_command_line>.

Adi Inbar
  • 268
  • 2
  • 8