我是Windows批处理cmd脚本的初学者,所以这可能是一个微不足道的问题,但谷歌没有给我答案。
当我在cmd或powershell提示符下运行以下命令时,它不会出错:
if([bool](Get-WmiObject -Class Win32_Share -ComputerName AMD -Filter "Path='I:\\popsi'") -ne $true) { net share popsi=i:\popsi /GRANT:Igor,FULL }
当在批处理脚本中运行相同的命令时,错误 -Class was unexpected at this time 已打印。我试图逃跑 -Class 作为 \-Class 但这并没有解决问题。该命令在批处理文件中应如何显示?
-Class was unexpected at this time
-Class
\-Class
此功能需要PowerShell。如果需要在cmd脚本中运行它,可以使用-command开关调用PowerShell,如下所示:
powershell -c "if (Get-WmiObject -Class Win32_Share -ComputerName AMD -Filter 'Path=''I:\\popsi''') { net share popsi=I:\popsi /GRANT:Igor,FULL }"
在这里 -c 缩写 -command 建议把开关打出来,我只是把它剪短了以提高可读性。 你不需要强制返回以下值 Get-WmiObject 到 $true ,这在 if 条款。
-c
-command
Get-WmiObject
$true
if
编辑:修复了报价问题。