in powershell, set affinity in start-process

删除回忆录丶 提交于 2021-01-28 03:42:55

问题


In powershell, I can launch a process with

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

I try set the affinity with

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

.... no work.

In windows powershell, when I launch a process how I can set the affinity ?


回答1:


I can solve with

$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:


You need to pass -PassThru switch in order to get process object

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

according to powershell Start-Process command (from ps 3.0)

-PassThru Returns a process object for each process that the cmdlet started. By default, this cmdlet does not generate any output.




回答3:


PowerShell Start Script

I missed the DOS start command so I combined @JuanPablo's code into a shell script called PSstart.ps1 that you can use to replace the start command in PowerShell.

Just use it like PowerShell -file PSStart.ps1 -affinity <affinity> -priority <priority> <path to executable> <executable arguments> Enjoy!


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");
}


来源:https://stackoverflow.com/questions/19250927/in-powershell-set-affinity-in-start-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!