1

I have a GUI that has been created with PowerShell Studio and exported as a PS1-file. I'm now trying to launch this GUI by calling it with another user's credentials.

When I run the code it doesn't even give an error message. PowerShell pops-up and closes again in seconds and nothing is launched. Follwoing this thread, I think I followed the correct syntax.

$Script = 'S:\Prod\Script center\GUI Script.ps1'
$Credentials = Get-Credential
$powershellArguments = "-file '$Script'", "-noexit", "-verb runas"
Start-Process powershell -Credential $Credentials -ArgumentList $powershellArguments

These ones doesn't work either:

Start-Process powershell -Credential $Credentials -ArgumentList "-noprofile -command &{Start-Process powershell -verb runas -File 'S:\Prod\Script center\GUI Script.ps1'}"
Start-Process powershell -Credential $Credentials -ArgumentList "-noprofile -command &{Start-Process $script -verb runas}"

And this one is asking me the credentials, although they are already saved in the variable $Credentials. However, the powershell console launched is not launched as the user in the Credentials :(

$cmd = 'powershell.exe'
$arguments = "-NoExit", "-NoProfile", "-WindowStyle Maximized", '-NoLogo', "Credential $Credentials", "File '$script'"
Start-Process $cmd -ArgumentList $arguments -Verb runAs

I'm sure it's not related to the GUI script, because this works perfectly fine:

& 'S:\Prod\Script center\GUI Script.ps1'

Any help is greatly appreciated.

1 Answer 1

1

Maybe your error is only on argument single quotes $powershellArguments = "-file '$Script'"; double quotes should be used.

Start-Process -FilePath "powershell" -Credential $cred -ArgumentList @("-file 'cred.ps1'") # doesn't work
Start-Process -FilePath "powershell" -Credential $cred -ArgumentList @("-file ""cred.ps1""") # works
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! This launches at least the GUI: $powershellArguments = "-File ""$script""", "-verb runas"; Start-Process powershell -Credential $Credentials -ArgumentList $powershellArguments. The only annoying thing is that the console window of the caller stays open, so the GUI app is visible with the PowerShell console behind it. Is there a workaround for this? I tried -NoNewWindow, but that didn't help.
You're starting a new PowerShell process as another user. It can't open in the same window since then it would be using your credentials, not the new ones. You're better off trying to close the old window once the new one is open.
Found the complete solution: $Arguments = "-File ""$Script""", "-verb runas"; Start-Process powershell -Credential $Credentials -ArgumentList $Arguments -WindowStyle hidden

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.