0
votes

So I've set up some credentials to be imported from an encrypted text file, an am trying to use them in conjunction with the 'start process' cmdlet. I'm new to powershell so cld be missing smoething obvious, but if i type Start-Process powershell.exe it opens a new ps windows as expected, however trying StartProcess powershell.exe -Credential $cred doesnt work. I know its not about not having the access rights on the credentials provided, cos I get hit with the error message Start-Process: This command cannot be run due to the error ; The filename or extension is too long This is confusing me as I havent changed the filename / source - all ive done is provided credentials. I've even confirmed that the $cred is indeed a credential, and if i type it into the terminal i do indeed get the username and System.Security.SecureString back.

Any Ideas?

Edit: My code looks like this:

[string]$pswd= Get-Content 'C:\Users\USER\pswd.txt'
[SecureString]$ss= $pswd | ConvertTo-SecureString -AsPlainText -Force
[string]$un ='USERNAME'
[PSCredential]$cred = New-Object System.Management.Automation.PSCredential ($un, $ss)
Start-Process powershell -Credential $cred 

And The Password is set like this:

[string]$Password='PASSWORD'
[SecureString]$SS = $Password | ConvertTo-SecureString -AsPlainText -Force
[string]$STR = $SS | ConvertFrom-SecureString
$STR | Set-Content -Path 'C:\Users\USER\pswd.txt'
1
Just checked starting notepad.exe while passing credentials of a test user into Start-Process and it worked fine. Can you share some code with us? - LordPupazz
I've added that in now, thanks for taking the time to respond! - E-A

1 Answers

0
votes

OK, the issue is the way you're saving the secure string to read later. Try this:

$pwd = "Password"
$pwds = $pwd | ConvertTo-SecureString -AsPlainText -Force
$pwdse = ConvertFrom-SecureString -SecureString $pwds
$pwdse | Out-File "C:\Users\User\pswd.txt"

Then to read it back in:

$pwd = Get-Content "C:\Users\User\pswd.txt" | ConvertTo-SecureString

From then on, you can create your PSCredential object the same as normal and it should work fine.