windows - powershell 传递命令行参数

标签 windows powershell powershell-2.0

这是我的代码:

$script={
 Write-Host "Num Args:" $args.Length;
  Write-Host $args[0]   
}

Invoke-Command  -ScriptBlock $script

当我运行时

  powershell.exe .\test.ps1 one two three

我得到了

Num Args: 0

我以为我会得到

Num Args: 3
One

我错过了什么吗?

谢谢

最佳答案

你实际上有两个范围。脚本级别和脚本 block 级别。 $args.Length$args[0] 将在 Invoke-Command 级别具有您所期望的。在脚本 block 内,$args 有另一个作用域。要从命令行一直获取参数到脚本 block 中,您需要从 Invoke-Command -ArgumentList $args 重新传递它们。

关于windows - powershell 传递命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185124/

相关文章:

C - 获取特定设备的 COM 端口号

c# - 在 Windows 中,如何使用 C++ 或 C# 在属于另一个进程的窗口中获取突出显示的文本?

windows - 在 Windows 7 上使用 PowerShell 从 COM 端口捕获输出

Powershell 以具有提升权限的另一个用户身份运行

c++ - codecvt_utf8<wchar_t> 被转换为原生 codecvt<wchar_t, char>

c++ - winsock:使用 localhost (127.0.0.1) 时连接失败并出现错误 10049

powershell - 远程运行命令时如何更改Powershell CLR版本?

windows - 在 Windows Server 数据中心找不到 WebAdministration powershell 模块

winforms - 如何通过 VBS 将变量从一个 PS 脚本传递到另一个 PS 脚本?

xml - 如何使用 PowerShell 更改 XML 元素属性的值?