Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using start-process to call other powershell file

EDIT::::See very bottom for current state of issue.

In the current set up, a batch file calls a powershell script with the following

powershell D:\path\powershellScript.v32.ps1 arg1 arg2 arg3 arg4

I would like to convert this into a powershell script calling another powershell. However, I'm having issues using start process. This is what I currently have but upon execute I get the following

No application is associated with the specified file for this operation

This is the powershell that is executing

$powershellDeployment = "D:\path\powershellScript.v32.ps1"
$powershellArguments = "arg1 arg2 arg3 arg4"
Start-Process $powershellDeployment -ArgumentList $powershellArguements -verb runas -Wait

EDIT::::::

Due to the help below, I now have the following

$username = "DOMAIN\username"
$passwordPlainText = "password"     
$password = ConvertTo-SecureString "$passwordPlainText" -asplaintext -force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password

$powershellArguments = "D:\path\deploy.code.ps1", "arg1", "arg2", "arg3", "arg4"
Start-Process "powershell.exe" -credential $cred  -ArgumentList $powershellArguments

However, when I execute this script from a remote machine I get "access denied" errors, even though the username used has full administrator access to the machine

like image 966
mhopkins321 Avatar asked Aug 20 '12 16:08

mhopkins321


2 Answers

Change this:

$powershellArguments = "arg1 arg2 arg3 arg4"

to

$powershellArguments = "arg1", "arg2", "arg3", "arg4"

The -ArgumentList parameter is expecting an array of arguments - not a single string with all the arguments.

like image 96
Keith Hill Avatar answered Oct 02 '22 09:10

Keith Hill


You should be using Start-Process powershell.exe, and passing the path to the script as the -File argument in your arg list. The No application... bit means that you don't have a default application set to work with .ps1 files on your machine. If you do the whole Right Click -> Open With -> Select Application -> check "Use this program as default..." tidbit on any .ps1 file, then the message goes away. My default program is notepad, so when I use Start-Process on a .ps1, it pops it up in that.

Edit:

To put it all together...

Start-Process powershell.exe -ArgumentList "-file C:\MyScript.ps1", "Arg1", "Arg2"

Or, if you define $powershellArguments as Keith says ($powershellArguments = "-file C:\MyScript.ps1", "arg1", "arg2", "arg3", "arg4"), then like this:

Start-Process powershell.exe -ArgumentList $powershellArguments
like image 23
SpellingD Avatar answered Oct 02 '22 09:10

SpellingD