0

I'm using powershell to start process as another user. To do this I'm using "Start-Process" as below.

$creds = Get-Credential -UserName $Name -Message "Please enter authorized credentials"

Start-Process `
    -Credential $creds `
    "C:\MyPowerfullProcess.exe" `
    -RedirectStandardOutput $logOutputPath `
    -RedirectStandardError $logErrorPath `
    -WorkingDirectory $workingPath `
    -ArgumentList $processArg
Stop-Transcript

The problem is that the $workingPath is only accessible to the user $creds and not by the user who started the .ps1 script. And The Start-Process command trough this error : "WorkingDirectory" Invalid [..] DirectoryNotFoundException

Start-Process

  • CategoryInfo : InvalidOperation : (:) [Start-Process], DirectoryNotFoundException
  • FullyQualifiedErrorId : DirectoryNotFoundException,Microsoft.PowerShell.Commands.StartProcessCommand
Boaepa
  • 1
  • 2

1 Answers1

0

Try to use the following -Verb addition to specify the run-as.

$creds = Get-Credential -UserName $Name -Message "Please enter authorized credentials"

Start-Process `
    -Credential $creds `
    "C:\MyPowerfullProcess.exe" `
    -RedirectStandardOutput $logOutputPath `
    -RedirectStandardError $logErrorPath `
    -WorkingDirectory $workingPath `
    -ArgumentList $processArg `
    -Verb RunAsUser
Stop-Transcript

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process

  • 1
    Thanks for your answer, with several tests it seams that my $workingPath is too long. -Verb RunAsUser paramter didn't correct the problem. In addition, -Verb RunAsUser seems not working with the RedirectStandard output/error parameters. (With error : Parameter set cannot be resolved using the specified named parameters) – Boaepa May 06 '22 at 07:06