I have a Windows server that will sometimes reboot into safe mode after updates. I'm working on that issue but what I'd really like to know is how can I check to see if Windows is running in safe mode or not.
Ideally I would like to incorporate it into a script that would send a passive check to our Nagios box with the status.
Is there some environmental variable I can use or some way to get this information via the command line?
I think this does what you are looking for
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102%28v=vs.85%29.aspx
Possible return values:
According to this article, an environment variable called
SAFEBOOT_OPTION
is set to eitherMinimal
orNetwork
if the system is started in Safe Mode or in Safe Mode with Networking; otherwise, the variable is unset.A test on the variable's value should do the trick; however, keep in mind that if the system is actually running in Safe Mode, it'll have no networking to begin with, so reporting its status could be... difficult.
You can also run the WMI query suggested by Craig620 directly from the command line, if you're not using PowerShell:
EDIT: my bad, I didn't read the KB thoroughly enough to realize it's basically useless as an answer on its own.
A more useful way to determine if you're in safe mode of not is from: Microsoft® Windows® Internals: Microsoft Windows ServerTM 2003, Windows XP, and Windows 2000 by Mark E. Russinovich, David A. Solomon.
Take the above and pair with the below, and you'll a have registry location you can check with a numerical value you can translate into something useful.
From the support.microsoft KB titled, "How to determine whether the system is running in Safe Mode from a device driver."
HKLM\SYSTEM\CurrentControlSet\Control\SystemStartOptions contains a string and if you are in safe mode there will be a "SAFEBOOT:???" within the string where ??? is MINIMAL or NETWORK. This gets updated on each boot.