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

标签 powershell start-process

大家晚上好

我正在使用命令行将参数传递给以下脚本中的变量,以便在我从该脚本中调用的另一个 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 - 因此您可以再次检查它或插入一些调试输出以查看脚本哪里出了问题

GIF showing working solution

关于powershell - 使用 PowerShell 的 Start-Process cmdlet 传递变量参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900961/

相关文章:

powershell - Autodesk Revit 的启动过程等待问题

c# - 如何在64位机器上不使用shell执行的情况下在C#中启动32位进程?

powershell - 有没有办法通过 start-process 将可序列化对象传递给 PowerShell 脚本?

powershell - 如何在Powershell中获取参数计数和参数字符串?

arrays - 管道完整的数组对象而不是一次一个数组项?

sql-server - 从 Powershell : null values inserted as "blank" 插入 SQL

powershell - 如何在 PowerShell 中启动和停止进程?

powershell - Get-WmiObject-查询不返回任何内容/为空

powershell 上的 Azure CLI 命令给出 '"c :\\\"Program' is not recognized as an internal or external command,

uwp - 使用Powershell启动Metro风格的应用程序