You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell 5中获取GetSystemTimeAdjustment值及Windows默认最小线程时间片?

在PowerShell 5中获取GetSystemTimeAdjustment值及查询Windows默认线程时间片

一、获取GetSystemTimeAdjustment值

PowerShell 5没有原生cmdlet直接调用这个Win32 API,我们可以通过Add-Type封装API调用实现:

Add-Type @"
using System;
using System.Runtime.InteropServices;

public class TimeAdjustment {
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool GetSystemTimeAdjustment(
        out long lpTimeAdjustment,
        out long lpTimeIncrement,
        out bool lpTimeAdjustmentDisabled
    );
}
"@

$timeAdjustment = 0
$timeIncrement = 0
$adjustmentDisabled = $false

$success = [TimeAdjustment]::GetSystemTimeAdjustment([ref]$timeAdjustment, [ref]$timeIncrement, [ref]$adjustmentDisabled)

if ($success) {
    Write-Host "时间调整值: $timeAdjustment"
    Write-Host "时间增量值: $timeIncrement"
    Write-Host "是否禁用时间调整: $adjustmentDisabled"
} else {
    Write-Error "调用失败,错误码: $([System.Runtime.InteropServices.Marshal]::GetLastWin32Error())"
}

输出的三个值说明:

  • 时间增量值($timeIncrement)单位为100纳秒,除以10000即可转换为毫秒,这就是你需要的线程结束前最短等待时间参考值。
  • 时间调整值表示系统主动调整时间的幅度,是否禁用时间调整标识系统是否开启了时间校准功能。

二、查询Windows默认最小线程时间片

有两种可靠的本地查询方法:

方法1:通过CIM查询系统属性

Get-CimInstance Win32_OperatingSystem | Select-Object QuantumLength, Caption
  • QuantumLength返回的就是当前系统默认线程时间片(单位:毫秒)
  • Caption会显示当前Windows版本,方便对应参考:比如Win10/11客户端默认前台线程时间片为10ms、后台为4ms,Server版本通常更长。

方法2:通过性能计数器读取

Get-Counter "\System\Thread Quantum Length" | Select-Object -ExpandProperty CounterSamples | Select-Object CookedValue

CookedValue字段直接返回当前系统的线程时间片长度(单位:毫秒)


内容的提问来源于stack exchange,提问作者jcolebrand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:50:17