Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ScriptBlock with different credentials

I have a script, that determines a userid; once I have that userid, I want to run a script block against that userid using different credentials. Is this possible? Can anyone show me examples of this?

like image 322
Bill Avatar asked Jan 25 '14 01:01

Bill


People also ask

How do I Run a PowerShell script with different credentials?

Then you can right-click on the PowerShell icon, which shows you an option as Windows PowerShell. Hovering over that options, Click Shift and right-click together to open another menu. You can choose Run as Different User from the new menu. Then a different popup would be opened, as shown in the below image.

How do I Run a PowerShell command remotely?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.

What is invoke-command?

The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.

What does Scriptblock do in PowerShell?

In the PowerShell programming language, a script block is a collection of statements or expressions that can be used as a single unit. A script block can accept arguments and return values. A script block returns the output of all the commands in the script block, either as a single object or as an array.


1 Answers

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.

like image 164
Bill Avatar answered Oct 25 '22 02:10

Bill