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

时间:2012-08-20 16:02:20

标签: powershell batch-file

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

在当前设置中,批处理文件使用以下

调用powershell脚本
powershell D:\path\powershellScript.v32.ps1 arg1 arg2 arg3 arg4

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

No application is associated with the specified file for this operation

这是正在执行的powershell

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

EDIT ::::::

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

$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

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

2 个答案:

答案 0 :(得分:18)

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

编辑:

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

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

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

Start-Process powershell.exe -ArgumentList $powershellArguments

答案 1 :(得分:5)

改变这个:

$powershellArguments = "arg1 arg2 arg3 arg4"

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

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