4

I have a main menu script where I ask for the user to enter admin credentials and I want to use them in other scripts that the main menu calls.

Here is my call to other scripts which launches the new script just fine and the called script runs.

Start-Process PowerShell.exe -ArgumentList "-noexit", "-command &$ScriptToRun -UserCredential $UserCredential"

In the called script I accept the parameters like this

#Accept User Credentials if sent from Main menu
param(
[parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true,Mandatory=$false)]
  [PSCredential]$UserCredential
)

Instead of accepting the credentials, though, it is launching the prompt asking for credentials with the name of the object in the user field.

enter image description here

Any help would be greatly appreciated in figuring this out, as I am banging my head against the wall.

1
  • 1
    Why are you using Start-Process to invoke new instances of powershell.exe? Commented Aug 8, 2019 at 18:16

2 Answers 2

2

You can't pass non-string object directly across process boundaries like that, the input arguments to the new process is always going to be bare strings.

What you can do instead is use $UserCredentials to simply launch the new process:

Start-Process PowerShell.exe -Credential $UserCredential -ArgumentList "-noexit", "-command &$ScriptToRun" 

... or, preferably, call Invoke-Command from the calling script instead of starting a new process:

Invoke-Command -ScriptBlock $scriptBlockToRun -Credential $UserCredential
Sign up to request clarification or add additional context in comments.

3 Comments

Because PowerShell is likely perfectly capable of solving your problem without the added complexity of spinning up multiple process (+ no performance overhead from spinning up multiple process) - and now you can meaningfully pass data between your scripts (not limited to credentials) without manually converting strings back and forth. In short - you get to actually take advantage of all the things that make PowerShell nice :)
If you want a more concise or meaningful answer, I'd suggest updating the question with what you need the credentials for - what kind of operations do you require elevation or impersonation for? :)
@ToddWelch The Start-Process approach works if your command doesn't have double quotes which mess up the command line parsing. In that case, investigate the -EncodedCommand parameter. The Invoke-Command approach only works if you include the -ComputerName parameter and enable PSRemoting even if you want to execute on localhost.
0

One simple way to launch a process with credentials is to use Jobs.

$UserCredential = Get-Credential
$ScriptBlock = {param($x) "Running as $(whoami).  x=$x"}
& $ScriptBlock 10          # Just check it works under current credentials
$Job = Start-Job -Credential $UserCredential -ScriptBlock $ScriptBlock -ArgumentList 20
Receive-Job -Job $Job -Wait -AutoRemoveJob

One advantage of using jobs is that you get Output, Error, Verbose and Warning streams passed back to the caller which is not possible when using Start-Process.

When using jobs all arguments and return values are implicitly serialized, which has limitations when passing complex objects, but copes well with all the basic types.

1 Comment

Jobs are not Processes; closing the starting terminal will kill all child jobs, but not processes.

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.