4

I am looking for prove or disprove of my theory. We have windows service which runs under LocalSystem account. the service is calling msbuild and builds csproj (Visual Studio project) file, which use Environment variable. But its falling as it cant recognize or find that environment variable.

Is there way how I can test or assure that win service running under LocalSystem has access to environment variables?

LazyOne
  • 3,014
  • 1
  • 16
  • 15
Jaroslav Urban
  • 143
  • 1
  • 3

2 Answers2

5

Programs running as SYSTEM (LocalSystem, NT_AUTHORITY\SYSTEM) will have an environment built from the variables specified at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and at HKEY_USERS\.DEFAULT\Environment. Make sure the variables you want it to "see" are defined in one of those locations and they will be accessible.

Evan Anderson
  • 141,071
  • 19
  • 191
  • 328
  • Session Manager\Environment key was lost on reboot? – Richard Hauer Jan 11 '17 at 12:09
  • I don't understand what you're saying / asking. No-- Windows will not just randomly remove registry values specified at that location. Perhaps you booted a different ControlSet, however. `CurrentControlSet` is a symlink to the booted ControlSet. – Evan Anderson Jan 13 '17 at 19:45
  • It wasn't really a question of if, more why. After setting that key and rebooting the value was gone. I assume it's built dynamically from data in other locations. I haven't checked yet whether adding the value to the `.Default\Environment` key was sufficient to solve that issue, but `HKLM`, while it works at first, appears to not survive a reboot. – Richard Hauer Jan 13 '17 at 20:33
2

If your environment variables are changing and you're running the service (as LocalSystem) in Windows Server 2003, XP, or 2000 then you might be running into the case that you need to restart the machine in order for the service manager to reread the environment. See this KB article for more info.

There's a hotfix available for Windows Server 2003 that will resolve this.

squillman
  • 37,618
  • 10
  • 90
  • 145