2

I have a working PS script that gets the boot time of remote servers

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

Result:

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

Are there any other details that can be extracted using PowerShell or another API to show that the server has properly booted?

codewario
  • 19,553
  • 20
  • 90
  • 159
Ruben_PH
  • 1,692
  • 7
  • 25
  • 42
  • 1
    This is on topic for SO, but worded poorly and at first glance does seem to be seeking help with system troubleshooting, though that language was superfluous. OP is asking for a programmatic way to identify current system initialization state from PowerShell. I have removed unnecessary text and cleared up the title so it is more clear what OP is asking for, and that the question remains on-topic for Stack Overflow. – codewario Oct 31 '21 at 01:42

1 Answers1

2

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.

There are boot states of Windows you can attempt to look for ways to check these but the most reliable way I've found to know if the computer is in a state for a user to log on is to wait for Get-Process winlogon to return a process back. Once winlogon is running, the computer is near or able to facilitate logon sessions, pending completion of GPO application.


This SuperUser answer explains how to use the Windows Performance Toolkit (part of the Windows SDK) to initiate a boot trace and disseminate its report, but that isn't the focus of this question or answer. I'm including it here to show that waiting for winlogon is the right way to identify when the system is ready for interaction; execution of winlogon is actually the final Windows boot phase and that answer exemplifies this.

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.

As for the boot phases of Windows I've only been able to generate a report of a boot trace using xbootmgr. I'm not sure there is a documented API (win32 or otherwise) exposed to the userspace to check for the current boot phase outside of the boot trace. At this time I can only recommend looking at environmental details to know the current boot phase (such as checking for the winlogon process), although I'm not familiar enough with the environments of the other boot phases to make additional recommendations here.


If I learn more information about the other phases I will update this answer.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • I will check on this first thing Monday to test and report back here – Ruben_PH Oct 31 '21 at 03:02
  • @Ruben_PH just to be clear, we can't help you troubleshoot your boot issue here. But programmatically we can offer you how to check whether your boot is hung or not by identifying the all-but-last portion of the boot process having begun. The [su] page I linked to does show how to use WPT to initiate a boot trace if you confirm winlogon is not running. If it is hanging, and if my memory serves correctly, you should be able to launch into safe mode, initiate the boot trace, and then let it do its thing. Once you have the report you should be able to see which portion of boot you are stuck on. – codewario Oct 31 '21 at 03:12
  • 1
    that is exactly what I am aiming for. I am not asking for help to troubleshoot the boot issue. I just need to know if the servers has "properly" booted via PS. – Ruben_PH Oct 31 '21 at 11:34
  • Then checking for `winlogon` running is the signal you need. I only check for that bit you could ensure gpupdate is no longer running as well since *technically* that is the last real thing the boot process does before you can log in – codewario Oct 31 '21 at 12:51
  • 1
    Checking winlogon to see if the server can now accept remote connections is working as described here though we are not able to test it against the scenario where the server is stuck on a black screen after reboot. With that said, I am marking this one as the correct answer. – Ruben_PH Nov 02 '21 at 17:04
  • Protip: enable boot logging with f8 if you can (f8 options are a bootloader thing) this should list what it's loading and there is a good chance you're getting hung up on a driver. – codewario Nov 02 '21 at 19:01