- Revenera Community
- :
- InstallShield
- :
- InstallShield Forum
- :
- Re: PowerShell script CustomAction not working
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
PowerShell script CustomAction not working
Hello everyone,
I've created a CustomAction that executes a PowerShell script. Here is the PowerShell script:
$installdir = get-property -name INSTALLDIR
$installdir | Out-File -FilePath C:\Test\testinstalldir.txt
new-service -Name TestService1 -DisplayName "Test Service 1" -Description "Test Service to check if it works" -BinaryPathName "$installdir\TestService\TestService.exe" -StartupType Automatic
As you can see, I'm getting the installdir as a string from the installshield property INSTALLDIR.
I put the CustomAction to execute after PublishProduct in the Install Exec Sequence. But when executing the installer, it's not working. It is actually outputting the file in C:\Test\, but it seems as if the statement after it fails. When executing that same statement manually on the machine, it is registering the service properly. Also, if I replace $installdir with a hardcoded path it is still not working, so it doesn't seem like that is the issue.
Is there a way to find out the error that is being thrown?
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
The problem is that I need to create a Windows Service and due to Dynamic File Linking I cannot define the proper Key File for my component and thus InstallShield cannot be used to install the Windows Service. I'm forced to use PowerShell for this.
Right now I wrapped the new-service line in a try catch which using Out-File prints out the error in a txt file.
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
Have you tried using the InstallScript function "ServiceAddService" for registering a service?
We use that in InstallScript projects when installing a user-defined number of Windows services.
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
I'm not familiar with InstallScript, can you also set the user which runs the windows service? If that's the case I'll have to dive into that.
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
We haven't had to do that specifically, but you should be able to do that via LaunchAppAndWait using cmd.exe with the correct sc config parameters.
https://stackoverflow.com/questions/308298/how-to-set-windows-service-username-and-password-through-commandline
We use this code to set a service to startup as Delayed instead of Automatic:
LaunchAppAndWait( WINSYSDIR ^ "cmd.exe", "/c sc config MyServiceName start= delayed-auto", WAIT | LAAW_OPTION_HIDDEN );