0

I want to call another secondary powershell script from within my primary powershell script. I want to pass it a parameter from the primary script, the secondary script needs the username parameter, I want to pass to it, from the primary, and then have the secondary script Im calling use different credentials. I think I might be able to use invoke-command, I just dont know all the syntax, anyone able to post some examples of what I want to accomplish, and then I'll fill in the blanks if need be? Thanks in advance! :-)

2 Answers 2

1

Assume that your secondary script looks like this:

param (
    [string] $Username = $args[0]
)
Write-Output -InputObject $Username;

You can use the Start-Process cmdlet to launch the script with alternate credentials.

$Credential = Get-Credential;
Start-Process -Wait -NoNewWindow -FilePath powershell.exe -ArgumentList '"c:\path\to my\file.ps1" -Username "UsernameGoesHere!"' -Credential $Credential;

Or you can use the Invoke-Command cmdlet:

Invoke-Command -FilePath 'c:\path\to my\script.ps1' -Credential $Credential -ArgumentList "UsernameGoesHere!";
Sign up to request clarification or add additional context in comments.

3 Comments

Where do I specify the parameter I am passing it from the primary script?? Thanks.
Thanks Trevor, I will try that, and let you know how it goes.
Okay, I just updated it again, since I noticed you edited your question. It looks like you need a Username parameter.
0

I got it, thanks to Trevor Sullivan for pointing me in the right direction. I ended up just putting my second ps1 file into a scriptblock, and running it as a job, and passing it the arguments from the main script, like this

$job = Start-Job -scriptblock {
 param ($username)
 some code to run against the variable that was passed in
 } -Args $target -credential $Cred

$target being the variable I want to pass to my scriptblock $username being the parameter that the scriptblock accepts Thanks.

Comments

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.