繁体   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