MustardFacial
MustardFacial

Reputation: 5

Run a powershell script with different credentials

I'm trying to run a powershell script to search for a network drive for a certain file. In my testing, I've found that my script works perfectly fine, however the network drive I need to search require my Domain Admin logon.

I have

Start-Process powershell.exe -Credential "domain\adminusername" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"

as the very first line of my script, but whenever I run the script I get this error:

Start-Process : This command cannot be run due to the error: The directory 
name is invalid.
At Path\to\script.ps1:1 char:1
+ Start-Process powershell.exe -Credential "domain\adminusername" -NoN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process], 
InvalidOperationException
+ FullyQualifiedErrorId : 
InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

What directory name is it talking about? If I move the script to the actual network drive, I still get the same error. How do you run a script as a different user?

Upvotes: 0

Views: 1825

Answers (1)

S. Hurley
S. Hurley

Reputation: 75

You could use the net use command to gain access or the new-psdrive command instead. Another option would be to start-process a cmd prompt and use runas within it. Also, you may need to include the full path of powershell.exe or add it to the path variable. %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Upvotes: 1

Related Questions