【问题标题】:in powershell, set affinity in start-process在powershell中,在启动过程中设置亲和力
【发布时间】:2013-10-08 14:45:41
【问题描述】:

在powershell中,我可以启动一个进程

$app_name = "app.exe"
$app_arguments = "arg0"
Start-Process $app_name $app_arguments

我尝试设置亲和力

$app = Start-Process $app_name $app_arguments
$app.ProcessorAffinity = 0x3

....没有工作。

在 windows powershell 中,当我启动一个进程时,我如何设置亲和力?

【问题讨论】:

    标签: windows powershell affinity


    【解决方案1】:

    我可以解决

    $app_name = "app.exe"
    $app_arguments = "arg0"
    
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo
    $pinfo.FileName = $app_name
    $pinfo.Arguments = $app_arguments
    $p = New-Object System.Diagnostics.Process
    $p.StartInfo = $pinfo
    $p.Start()
    $p.ProcessorAffinity=0x3
    

    【讨论】:

      【解决方案2】:

      需要通过 -PassThru 开关才能获取进程对象

      $app = Start-Process $app_name $app_arguments -PassThru
      $app.ProcessorAffinity = 0x3
      

      根据powershell Start-Process command (from ps 3.0)

      -PassThru 为 cmdlet 启动的每个进程返回一个进程对象。默认情况下,此 cmdlet 不会生成任何输出。

      【讨论】:

        【解决方案3】:

        PowerShell 启动脚本

        我错过了 DOS 的 start 命令,因此我将 @JuanPablo 的代码组合成一个名为 PSstart.ps1 的 shell 脚本,您可以使用它来替换 PowerShell 中的 start 命令。

        就像使用它PowerShell -file PSStart.ps1 -affinity <affinity> -priority <priority> <path to executable> <executable arguments>享受吧!


        param([Int32]$affinity=0xF,[String]$priority="NORMAL", [String]$appPath="", [String]$appArguments="")
        [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")   # For message box error reports, remove if you don't want popup errors
        
        $priorityValues = "LOW", "NORMAL", "HIGH", "REALTIME", "ABOVENORMAL", "BELOWNORMAL" # Remove ABOVENORMAL and BELOWNORMAL if running on Win98 or WinME
        $priorityUC = $priority.ToUpper()
        $pinfo = New-Object System.Diagnostics.ProcessStartInfo
        
        If($appPath -ne "" -and (Test-Path $appPath))
        {
            If($priorityValues -contains $priorityUC)
            {
                Try
                {
                    $pinfo.FileName = $appPath
                    $pinfo.Arguments = $app_arguments
                    $p = New-Object System.Diagnostics.Process
                    $p.StartInfo = $pinfo
                    $p.Start()
                    $p.PriorityClass=$priorityUC
                    $p.ProcessorAffinity=$affinity
                }
                Catch
                {
                    $exceptionMessage = $_.Exception.Message
                    #Write-Host "An exception:`n`n$exceptionMessage`n`noccured!" -fore white -back red  # Uncomment for console errors
                    [System.Windows.Forms.MessageBox]::Show("An exception:`n`n$exceptionMessage`n`noccured!", "An Exception Occured", "Ok", "Error");
                    Break
                }
            }
            Else
            {
                #Write-Host "The priority: `"$priorityUC`" is not a valid priority value!" -fore white -back red    # Uncomment for console errors
                [System.Windows.Forms.MessageBox]::Show("The priority: `"$priorityUC`" is not a valid priority value!", "A Priority Error Occured", "Ok", "Error");
            }
        }
        Else
        {
            #Write-Host "The application path: `"$appPath`" doesn't exist!", "A Path Error Occured" -fore white -back red   # Uncomment for console errors
            [System.Windows.Forms.MessageBox]::Show("The application path: `"$appPath`" doesn't exist!", "A Path Error Occured", "Ok", "Error");
        }
        

        【讨论】:

        • 我也想做同样的事情,但我不想运行 exe,而是想运行 ps1 脚本。但是,您的脚本正在寻找一个 exe。我该如何解决这个问题?
        猜你喜欢
        • 2022-12-05
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        相关资源
        最近更新 更多