Search code examples
powershellcredentials

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?


Solution

  • 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.