Running a script from another script as another user

Hi all,

I’m trying to run another script from a main script as a different user then redirect the output to a file. I can’t work out what I’m doing wrong, the error is “the directory name is invalid”. Please help!

$logpath = "\\Network\scripts\logs"
$rand = get-random -maximum 50
$filepath = "\\Network\NewUserScript.ps1"

Start-Process powershell -Credential (get-credential) -ArgumentList '-noexit','-file',$filepath,'> $logpath\transcript$rand.txt'

 

Two issues…

  1. The way how arguments are passed.

  2. Output redirection

  3. How to pass arguments.

Start-Process -FilePath PowerShell.exe -ArgumetnList "-NoExit -File $FilePath" 
  1. Redirection of output, you can use RedirectStandardOutput Parameter
Start-Process -FilePath PowerShell.exe -ArgumetnList "-NoExit -File $FilePath" -RedirectStandardOutput "$logpath\transcript$rand.txt"

Thanks for your reply kvprasoon, I didn’t know about the -redirectoutput switch. Now I have;

Start-Process powershell -Credential (get-credential) -ArgumentList "-noexit -file $filepath" -RedirectStandardOutput "$logpath\transcript$rand.txt"

Unfortunately this still yields the “directory name is invalid” error.

This should work. can you share the exact error you get.

It’s working now, the second user didn’t have the drive mapped the first user was running it from.

 

Thanks for the assistance.