powershell - 当命令行参数提供变量时,Powershell 命令调用不起作用
问题描述
我正在尝试为“net accounts”命令提供命令行参数。这在写出没有变量的命令时有效,但在传递带有变量的命令行参数时无效。
这是一个演示脚本:
Write-Host "--- As variable"
$Sw = "/lockoutduration:15 /lockoutwindow:15"
Write-Host $Sw
&net.exe accounts $Sw
Write-Host "--- Without variable"
&net.exe accounts /lockoutduration:15 /lockoutwindow:15
输出:
> .\Test.ps1
--- As variable
/lockoutduration:15 /lockoutwindow:15
You entered an invalid value for the /LOCKOUTDURATION option.
More help is available by typing NET HELPMSG 3952.
--- Without variable
The command completed successfully.
提供一组命令行参数的方法是什么?
操作系统:Windows 10 Pro 20H2,Powershell 版本:5.1
解决方案
向程序发送多个参数时,您需要将它们作为数组传递。尝试:
Write-Host "--- As variable"
$Sw = @("/lockoutduration:15","/lockoutwindow:15")
$Sw
&net.exe accounts $Sw
推荐阅读
- function - 使用用户定义函数错误的归约算子
- python - 如何处理文件中的字符串和整数?
- javascript - 在帖子中等待请求
- javascript - 如何自动化从一长串 URL 中获取 SPAN 文本的任务?
- javascript - 使用表单数据上传图片并在 React js 中返回前端
- css - 我可以为不包含链接的 div 编写 CSS 选择器吗
- c# - 不一致的可访问性属性类型难以访问
- python-3.x - 在 3 MultiIndex pandas 数据框中按 2 个索引检索行 - Python3
- r - 使用 rvest 同意 cookie
- javascript - 如何计算我应该从特定值中减去多少次?