使用PowerShell的Start-Process cmdlet传递变量参数 [英] Passing variable arguments using PowerShell's Start-Process cmdlet
问题描述
大家晚上好
我正在使用一个命令行,该命令行在以下脚本中将参数作为变量传递给变量,以便在我从此脚本中调用的另一个ps1中运行.每当我尝试从命令行传递参数时,我都会收到以下错误
I'm using a command line that passes arguments to as variables in the following scripts to be run in another ps1 that I'm calling from within this script. Whenever I try to pass the arguments from the command line I get the following error though
Start-Process:找不到可以接受的位置参数 论点
Start-Process : A positional parameter cannot be found that accepts argument
任何人都可以提供帮助吗?
感谢您的宝贵时间,非常感谢您的帮助.
Would anyone be able to assist?
Thank you for your time and much appreciate any help.
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-因此您可以再次对其进行检查或插入一些调试输出以查看脚本哪里出了问题
Update due to comment: To show you that it is working see the GIF below - so you may check it again or insert some debug output to see where things go wrong with your script
这篇关于使用PowerShell的Start-Process cmdlet传递变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!