Windows + Powershell:如何更改所有 java.exe 实例的处理器关联性?

ada*_*itj 0 windows cpu powershell windows-server-2012

基于StackOverflow 上的这个问题,如果可执行文件仅在 1 个实例中运行,我可以使用以下命令更改处理器关联性:

PowerShell "$Process = Get-Process java; $Process.ProcessorAffinity=11"
Run Code Online (Sandbox Code Playgroud)

如果有 2 个或更多实例正在运行,我无法更改,这是输出

C:\PowerShell "$Process = Get-Process java; $Process.ProcessorAffinity=11"
The property 'ProcessorAffinity' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:30
+ $Process = Get-Process java; $Process.ProcessorAffinity=11
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
Run Code Online (Sandbox Code Playgroud)

有谁知道如何使用 Powershell 更改所有 java.exe 实例的处理器关联性?

Sim*_*onS 5

您必须循环每个对象才能设置其ProcessorAffinity

| % {}PowerShell中的含义与其他语言中的语句ForEach-Object基本相同foreach()

正如 root 所说,您可以删除该变量,以便您的代码变得更短。

从 cmd 窗口:

PowerShell "get-process java | % { $_.ProcessorAffinity=11 }"
Run Code Online (Sandbox Code Playgroud)

在批处理文件中(批处理文件%像变量一样处理,因此您需要写入两次或切换到foreach):

PowerShell "get-process java | %% { $_.ProcessorAffinity=11 }"
PowerShell "get-process java | foreach { $_.ProcessorAffinity=11 }"
Run Code Online (Sandbox Code Playgroud)

直接在 PowerShell 中:

get-process java | % { $_.ProcessorAffinity=11 }
Run Code Online (Sandbox Code Playgroud)