lag*_*lex 5 powershell process powershell-3.0
有一个 powershell 脚本可以将进程的优先级从“空闲”设置为“实时”,但某些工具提供了另一个优先级,该优先级甚至会降低进程的优先级:

如何在 Powershell 中进行设置?
我不清楚是否可以设置IO优先级。SetProcessInformation() 调用采用 PROCESS_INFORMATION_CLASS 作为参数,并且仅定义 ProcessMemoryPriority。我遇到了一个 powershell 脚本从内存优先级为 2 的任务管理器中运行出来的问题,这让我很恼火。我是 PInvoke 的新手,并从 PowerShell 中使用它,因此我可能违反了至少一项最佳实践,但下面的代码片段解决了我的问题。
通过 C# 加载函数的 Add-Type 内容:
Add-Type @"
using System;
using System.Runtime.InteropServices;
namespace SysWin32
{
public enum PROCESS_INFORMATION_CLASS
{
ProcessMemoryPriority,
ProcessInformationClassMax,
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MEMORY_PRIORITY_INFORMATION
{
public uint MemoryPriority;
}
public partial class NativeMethods {
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint="GetCurrentProcess")]
public static extern System.IntPtr GetCurrentProcess();
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint="SetProcessInformation")]
public static extern bool SetProcessInformation(System.IntPtr hProcess, PROCESS_INFORMATION_CLASS ProcessInformationClass, System.IntPtr ProcessInformation, uint ProcessInformationSize) ;
}
}
"@
Run Code Online (Sandbox Code Playgroud)
这是使用函数的示例:
$myProcessHandle = [SysWin32.NativeMethods]::GetCurrentProcess()
$memInfo = New-Object SysWin32.MEMORY_PRIORITY_INFORMATION
$memInfo.MemoryPriority = 5
$memInfoSize = [System.Runtime.Interopservices.Marshal]::SizeOf($memInfo)
$memInfoPtr = [System.Runtime.Interopservices.Marshal]::AllocHGlobal($memInfoSize)
[System.Runtime.Interopservices.Marshal]::StructureToPtr($memInfo, $memInfoPtr, $false)
$result = [SysWin32.NativeMethods]::SetProcessInformation($myProcessHandle, [SysWin32.PROCESS_INFORMATION_CLASS]::ProcessMemoryPriority, $memInfoPtr, $memInfoSize)
$result
Run Code Online (Sandbox Code Playgroud)
使用 procexp,我能够验证我的 powershell 脚本现在是否以内存优先级 5 运行。
| 归档时间: |
|
| 查看次数: |
6388 次 |
| 最近记录: |