Search code examples
windowsvbscriptregistrysrvany

Cannot read registry key when using SRVANY


I am working on a script which needs to read and write a certain value to the HKLM registry hive. While it works fine when running as a script, registry access is denied when I am running the same code as a service using SRVANY utility. The service created using SRVANY runs under the Local System context which I belive should have full access to the HKLM registry hive. When running the code as a script, it is running under an administrator account.

REG_KEY_PATH = "HKLM\SOFTWARE\AppName\ServiceConfig"
strFilename = objWshShell.RegRead(REG_KEY_PATH)

The error that I get for the above code is

Invalid root in registry key "HKLM\SOFTWARE\AppName\ServiceConfig"


Solution

  • The trick was to use "c:\windows\sysnative\cscript.exe" to launch cscript via srvany. Full credit to Hans Passant for the answer.