如何使用 PowerShell 确认系统初始化完成?

How to confirm system initialization is complete using using PowerShell?

我有一个有效的PS脚本可以获取远程服务器的启动时间

#$ServerArray is the list of servers

$cso = New-CimSessionOption -Protocol Dcom
$cs = New-CimSession -ComputerName $ServerArray -SessionOption $cso
Get-CimInstance -CimSession $cs -ClassName win32_operatingsystem -OperationTimeoutSec 10 | select csname, Caption, lastbootuptime | sort lastbootuptime | format-table -autosize

结果:

csname         Caption                                   lastbootuptime
------         -------                                   --------------
server1        Microsoft Windows Server 2012 R2 Standard 10/30/2021 3:07:23 AM
server2        Microsoft Windows Server 2012 R2 Standard 10/30/2021 3:12:35 AM
server3        Microsoft Windows Server 2012 R2 Standard 10/30/2021 3:13:34 AM

是否可以使用 PowerShell 或其他 API 提取任何其他详细信息以表明服务器已正确启动?

Note: This may be difficult to use PowerShell for depending on the mechanism of execution; PowerShell Remoting is not available during early stages of boot, nor is an interactive CLI. However, some agents or services (like VMware Tools as an example) can facilitate remote execution at times like these. Scheduled Tasks can also be leveraged to run code locally during some earlier boot phases. Though this answer centers around PowerShell, the general information can be used with other programming languages as well.

有 Windows 的启动状态,您可以尝试寻找检查这些状态的方法,但我发现了解计算机是否处于用户登录状态的最可靠方法是等待 Get-Process winlogon 到 return 一个进程返回。一旦 winlogon 为 运行,计算机即将或能够促进登录会话,等待 GPO 应用程序完成。


SuperUser answer 解释了如何使用 Windows Performance Toolkit(Windows SDK 的一部分)启动启动跟踪并传播其报告,但这不是这个问题或答案的重点。我将其包含在这里是为了表明等待 winlogon 是确定系统何时准备好进行交互的正确方法; winlogon 的执行实际上是最后的 Windows 启动阶段,这个答案就是例证。

Note: Technically, logging in and waiting for scheduled tasks to complete on login is the final step but that portion comes after after kernel boot has completed, and can be repeated for multiple logon sessions. I don't consider this part of the "boot sequence" or system initialization.

至于 Windows 的引导阶段,我只能使用 xbootmgr 生成引导跟踪报告。我不确定是否有记录的 API(win32 或其他)暴露给用户空间以检查引导跟踪之外的当前引导阶段。目前我只能建议查看环境细节来了解当前的启动阶段(比如检查winlogon进程),虽然我对其他启动阶段的环境还不够熟悉,无法提出额外的建议这里。


如果我了解有关其他阶段的更多信息,我将更新此答案。