windows – powershell传递命令行参数
发布时间:2020-12-14 04:17:55 所属栏目:Windows 来源:网络整理
导读:这是我的代码: $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: 3One 我错过了什么? 谢谢 你实际
这是我的代码:
$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 我错过了什么? 谢谢
你实际上有两个范围.脚本级别和脚本块级别. $args.Length和$args [0]将具有您在Invoke-Command级别所期望的内容.在脚本块中,还有另一个$args的范围.要从命令行获取args一直到脚本块,您需要从Invoke-Command -ArgumentList $args重新传递它们.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |