调用 powershell 的 cmd 命令没有输出
No output from cmd command calling powershell
也许这是一个粗略的问题,但我在 powershell 中有一个代码 returns CPU 每个逻辑 CPU 核心的用法:
(Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };
在 powershell 中,此代码 returns 如下:
0: 0
1: 6
10: 6
11: 0
2: 0
3: 19
4: 0
5: 12
6: 0
7: 0
8: 0
9: 12
_Total: 4
现在我一直在尝试通过格式化命令来避免错误,从而在命令提示符中获得相同的输出运行。这就是我现在所拥有的:
在 CMD 中:
Powershell.exe 'Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor"' ^| foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };
哪个returns:
:
这与 powershell 输出不同。我几乎可以肯定这是由于某种格式问题造成的,但问题是什么?
- 阅读
PowerShell -Help
以获得 PowerShell.exe
个有效参数。
-Command
…
If the value of Command is a string, Command must be the last parameter
in the command , because any characters typed after the command are
interpreted as the command arguments.
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
- 注意内部双引号被转义为
powershell -nopro -command "& {(Get-WmiObject -Query \"select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor\") | foreach-object { write-host \"$($_.Name): $($_.PercentProcessorTime)\" };}"
或
powershell -nopro -command "& {(Get-WmiObject -Query """"select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor"""") | foreach-object { write-host """"$($_.Name): $($_.PercentProcessorTime)"""" };}"
您可以转义内部双引号:
"%__AppDir__%\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "(Get-WmiObject -Query \"Select Name,PercentProcessorTime From Win32_PerfFormattedData_PerfOS_Processor\")|ForEach-Object{\"$($_.Name): $($_.PercentProcessorTime)\"}"
您会注意到我删除了 Write-Host
命令,因为不需要它。
也许这是一个粗略的问题,但我在 powershell 中有一个代码 returns CPU 每个逻辑 CPU 核心的用法:
(Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };
在 powershell 中,此代码 returns 如下:
0: 0
1: 6
10: 6
11: 0
2: 0
3: 19
4: 0
5: 12
6: 0
7: 0
8: 0
9: 12
_Total: 4
现在我一直在尝试通过格式化命令来避免错误,从而在命令提示符中获得相同的输出运行。这就是我现在所拥有的: 在 CMD 中:
Powershell.exe 'Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor"' ^| foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };
哪个returns:
:
这与 powershell 输出不同。我几乎可以肯定这是由于某种格式问题造成的,但问题是什么?
- 阅读
PowerShell -Help
以获得PowerShell.exe
个有效参数。
-Command … If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments. To write a string that runs a Windows PowerShell command, use the format: "& {<command>}" where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.
- 注意内部双引号被转义为
powershell -nopro -command "& {(Get-WmiObject -Query \"select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor\") | foreach-object { write-host \"$($_.Name): $($_.PercentProcessorTime)\" };}"
或
powershell -nopro -command "& {(Get-WmiObject -Query """"select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor"""") | foreach-object { write-host """"$($_.Name): $($_.PercentProcessorTime)"""" };}"
您可以转义内部双引号:
"%__AppDir__%\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "(Get-WmiObject -Query \"Select Name,PercentProcessorTime From Win32_PerfFormattedData_PerfOS_Processor\")|ForEach-Object{\"$($_.Name): $($_.PercentProcessorTime)\"}"
您会注意到我删除了 Write-Host
命令,因为不需要它。