使用start-process调用其他powershell文件的问题

mho*_*321 9 powershell batch-file

编辑::::了解当前的问题状态.

在当前设置中,批处理文件使用以下内容调用powershell脚本

powershell D:\path\powershellScript.v32.ps1 arg1 arg2 arg3 arg4
Run Code Online (Sandbox Code Playgroud)

我想将此转换为一个名为另一个PowerShell的powershell脚本.但是,我在使用启动过程时遇到问题.这是我目前所拥有的,但在执行时,我得到以下内容

No application is associated with the specified file for this operation
Run Code Online (Sandbox Code Playgroud)

这是正在执行的powershell

$powershellDeployment = "D:\path\powershellScript.v32.ps1"
$powershellArguments = "arg1 arg2 arg3 arg4"
Start-Process $powershellDeployment -ArgumentList $powershellArguements -verb runas -Wait
Run Code Online (Sandbox Code Playgroud)

编辑::::::

由于下面的帮助,我现在有以下内容

$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
Run Code Online (Sandbox Code Playgroud)

但是,当我从远程计算机执行此脚本时,我收到"访问被拒绝"错误,即使使用的用户名具有对该计算机的完全管理员访问权限

Spe*_*ngD 20

您应该使用Start-Process powershell.exe,并将路径作为-Filearg列表中的参数传递给脚本.该No application...位表示您没有将默认应用程序设置为在计算机上使用.ps1文件.如果你Right Click -> Open With -> Select Application -> check "Use this program as default..."在任何.ps1文件上执行整个过程,那么消息就会消失.我的默认程序是记事本,所以当我Start-Process在.ps1上使用时,会弹出它.

编辑:

把它们放在一起......

Start-Process powershell.exe -ArgumentList "-file C:\MyScript.ps1", "Arg1", "Arg2"
Run Code Online (Sandbox Code Playgroud)

或者,如果你定义$powershellArguments为Keith说($powershellArguments = "-file C:\MyScript.ps1", "arg1", "arg2", "arg3", "arg4"),那么像这样:

Start-Process powershell.exe -ArgumentList $powershellArguments
Run Code Online (Sandbox Code Playgroud)


Kei*_*ill 5

更改此:

$powershellArguments = "arg1 arg2 arg3 arg4"
Run Code Online (Sandbox Code Playgroud)

$powershellArguments = "arg1", "arg2", "arg3", "arg4"
Run Code Online (Sandbox Code Playgroud)

-ArgumentList参数需要一个参数数组-不是所有参数都包含一个字符串。