簡體   English   中英

在PowerShell中,在啟動過程中設置親和力

[英]in powershell, set affinity in start-process

在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中,當我啟動一個進程時如何設置親和力?

我可以解決

$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

PowerShell啟動腳本

我錯過了DOS start命令,因此我將@JuanPablo的代碼組合到了一個名為PSstart.ps1的外殼程序腳本中,可用於替換PowerShell中的start命令。

就像PowerShell -file PSStart.ps1 -affinity <affinity> -priority <priority> <path to executable> <executable arguments>一樣使用它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");
}

您需要通過-PassThru開關才能獲取過程對象

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

根據powershell Start-Process命令(從ps 3.0開始)

-PassThru為cmdlet啟動的每個進程返回一個進程對象。 默認情況下,此cmdlet不會生成任何輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM