使用PowerShell的Start-Process cmdlet传递变量参数

时间:2016-08-11 15:48:51

标签: powershell start-process

大家晚上好,

我使用命令行将参数传递给以下脚本中的变量,以便在我从此脚本中调用的另一个ps1中运行。每当我尝试从命令行传递参数时,我得到以下错误

  

Start-Process:找不到接受的位置参数   参数

有人能帮忙吗? 感谢您的时间,非常感谢任何帮助。

param
(
    [string]$targetserver = $args[0], #target server
    [string]$module = $args[1], #module name
)

function Get-Script-Directory
{    
  return Split-Path $script:MyInvocation.MyCommand.Path
}

Start-Process powershell.exe (Join-Path (Get-Script-Directory) "...\StopServices.ps1") -ArgumentList $targetserver $module

1 个答案:

答案 0 :(得分:3)

尝试使用最后一行

$scriptPath = Join-Path (Get-Script-Directory) "...\StopServices.ps1"
Start-Process powershell.exe -ArgumentList "-file $scriptPath", $targetserver, $module  

由于评论而更新:要向您显示它正在工作,请参阅下面的GIF - 因此您可以再次检查它或插入一些调试输出以查看脚本出现问题的地方

GIF showing working solution

相关问题