我有以下PowerShell命令:
Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" |
Select-Object 'Name' |
Out-File C:\temp\ost.txt -Append
但是我需要在命令提示符下运行它。我正在像这样运行它:
powershell.exe -ExecutionPolicy ByPass -Command "Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'ost'" | Select-Object 'Name' | Out-File C:\temp\ost.txt -Append"
我收到此错误:
Get-WmiObject : A positional parameter cannot be found that accepts argument '*'. At line:1 char:1 + Get-WmiObject -Query Select * from CIM_DataFile Where Extension = 'os ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
如何正确运行?
答案 0 :(得分:2)
您必须转义 嵌套 "
字符。在您的命令中,该操作最可靠地通过\""
(原文如此)完成:
PowerShell.exe -c "Get-WmiObject -Query \""Select * ... 'ost'\"" | Select ..."
注意事项:\""
与powershell.exe
(对于PowerShell Core 为pwsh
)一起使用时效果很好,功能强大 other 程序,例如python
,ruby
,perl
或node
。
有关详细说明,请参见linked answer,包括如何转义其他程序。