Search code examples
windowsinstallationnsisgpo

Installer that adds entry to GPO Startup scripts


I'm in the process of writing an installer using NSIS that will add an entry to the computer's GPO to run a command on system boot.

# From http://nsis.sourceforge.net/LGP_Startup/Shutdown_Script
!include LGPScript.nsh

Section
  SetOutPath -

  # Install all files from myprog directory into C:\Program Files\myprog
  File myprog\*

  # Copy mysystweak.bat to System32\GroupPolicy\Machine\Scripts\Startup
  SetOutPath $SYSDIR\GroupPolicy\Machine\Scripts\Startup
  File myprog\mysystweak.bat
  SetOutPath -

  # Add GPO entry to execute mysystweak.bat on startup
  ${LGPScript::Create} 'Startup' 'mysystweak.bat' '' $R1
  DetailPrint "Create startup LGP return code:$R1"

  # Write uninstaller
  Writeuninstaller "${uninstall_name}"
SectionEnd

Everything goes well except nothing shows up in …\Startup. The installer thinks everything worked:

Output folder: C:\WINDOWS\system32\GroupPolicy\Machine\Scripts\Startup
Extract: mysystweak.bat... 100%
Output folder: C:\Program Files (x86)\myprog

I originally tried using CopyFiles to copy the batch file from $INSTDIR into …\Startup, but got the same result.

What's going wrong here?

related: https://serverfault.com/q/266338/2101


Solution

  • If this is a x64 system you need to disable FS redirection, without it $SYSDIR will not point to the true system32 directory:

    !include x64.nsh
    
    Section
    ...
    SetOutPath $SYSDIR\GroupPolicy\Machine\Scripts\Startup
    ${DisableX64FSRedirection}
    File myprog\mysystweak.bat
    ${EnableX64FSRedirection}
    ...
    SectionEnd
    

    You can verify that this is the problem by watching the installer with Process Monitor