Search code examples
.netpowershellboot

Is there a way to determine the current boot phase in Powershell?


I have a need to ensure that a Powershell script which may be executed during an early portion of the Windows boot phase waits until the Winlogon Init or Post Boot phase to continue executing. Is there a way to determine the current boot phase in Powershell/.NET? P/Invoke is acceptable if necessary.


Solution

  • Not really, the problem is that the complete boot process is asynchronous. You might determine that the logon screen is shown as Mathias suggested by checking the winlogon.exe or even if at least one user is logged on by e.g. checking userinit.exe has been started:

    Register-WmiEvent -Query SELECT * FROM Win32_ProcessStartTrace WHERE ProcessName='userinit.exe'" -Action {...}
    

    But even that doesn't automatically imply that e.g. the network is up and running, that the internet or the domain can be reached. This is because since windows 7, Microsoft has fast user logon enabled by default (see also: Understand the Effect of Fast Logon Optimization and Fast Startup on Group Policy). This basically means that in of lot of cases the user is already logged on (using cached credentials) prior the network connection is made...

    So if you script depends on e.g. a domain, you will need to create an event to capture a network change:

    Register-ObjectEvent ([System.Net.NetworkInformation.NetworkChange]) 'NetworkAddressChanged' -Action {...}
    

    And than ping to domain to confirm that the domain is really available...

    Conclusion

    Instead of trying to define the "current boot phase", you will need to define the dependencies of your cmdlet and check them (or build specific events) accordingly.