JFLarvoire / SysToolsLib

A library of Windows and Linux system management tools
Apache License 2.0
327 stars 95 forks source link

Recommendation how to check if running as SYSTEM, ie. invoked as a service #4

Closed GonzalezAdrian closed 8 years ago

GonzalezAdrian commented 8 years ago

Hi

Very nice Template!!

The implementation for the check if running as System, work's only in englisch OS version. The problem are the names of builtin system accounts in other languages. In english "NT AUTHORITY\SYSTEM" in german as example is "NT-AUTORITÄT\SYSTEM".

So on systems with german OS installed, the Service do not start if you use the System account to start the service.

The issue is on line 756 $isSystem = ($userName -eq "NT AUTHORITY\SYSTEM"

A better solution is: $isSystem = $identity.IsSystem

Regards Adrian

nischl commented 8 years ago

What if the service is running under a different account. We need a different approach to see whether we run as a service or not.

My approach is to have two copies of the script, one in the installation dir and another in my personal script dir where i actually use it interactively.

So I can check with: $isSystem = ($scriptCopy -eq $scriptFullName)

JFLarvoire commented 8 years ago

Thanks Adrian for the suggestion on how to detect the system account in a language-independent way.
I've just checked in an update with this change.

Nischl, I'm a bit wary of using a trick like that. So I'll stick with Adrian's proposal, even if it seems less general than yours.

Regards, Jean-François

nischl commented 8 years ago

thats fine as i wouldn't want that trick in your script anyway, because it is nothing more than an asking-for-user-error workaround. I just wanted to point out that the actual goal is to find out whether we run as a service or not :) Adrians solution for system is perfect (for me as I am running on german systems, too)