cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
gomochainstall
Level 6

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?

Labels (1)
0 Kudos
(4) Replies

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. 

0 Kudos

Have you tried using the InstallScript function "ServiceAddService" for registering a service?

https://docs.revenera.com/installshield19helplib/Subsystems/installshield19langref/helplibrary/ServiceAddService.htm

We use that in InstallScript projects when installing a user-defined number of Windows services.

0 Kudos

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.

0 Kudos

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://docs.revenera.com/installshield19helplib/Subsystems/installshield19langref/helplibrary/LangrefLaunchAppAndWait.htm

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 );
0 Kudos