大家晚上好
我正在使用命令行将参数传递给以下脚本中的变量,以便在我从该脚本中调用的另一个 ps1 中运行。每当我尝试从命令行传递参数时,我都会收到以下错误
Start-Process : A positional parameter cannot be found that accepts argument
有人能帮忙吗?
感谢您的宝贵时间,非常感谢您的帮助。
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
最佳答案
在最后一行试试这个
$scriptPath = Join-Path (Get-Script-Directory) "...\StopServices.ps1"
Start-Process powershell.exe -ArgumentList "-file $scriptPath", $targetserver, $module
由于评论而更新:为了向您展示它正在工作,请参见下面的 GIF - 因此您可以再次检查它或插入一些调试输出以查看脚本哪里出了问题
关于powershell - 使用 PowerShell 的 Start-Process cmdlet 传递变量参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900961/