Startup.pss seems to only run the first time

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Startup.pss seems to only run the first time

Post by falconcurt »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: Powershell Studio 2017 5.4.143
32 or 64 bit version of product: 64 bit
Operating system: Windows 7
32 or 64 bit OS: 64 bit

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

I added an If statement within the function Main in the Startup.pss file. It checks if the file doesn't exists, create file. It runs the first time when i install the project on a new computer but doesn't seem to work when i uninstall the application and reinstall. I have verified that the file doesn't exists but it still doesn't run after the first time. Can i get a little more explanation on the startup.pss file and the function Main? Should it run every time I execute the .exe file?

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Startup.pss seems to only run the first time

Post by davidc »

The Main function is called every time the script is ran, but without your code it is hard to tell what is happening.
David
SAPIEN Technologies, Inc.

User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Re: Startup.pss seems to only run the first time

Post by falconcurt »

I also want to state that it works correctly when i just run the project with Powershell Studio but when i use the packaged executable it only works the first time. Here is the code in the startup.pss file

function Main {
<#
.SYNOPSIS
The Main function starts the project application.

.PARAMETER Commandline
$Commandline contains the complete argument string passed to the script packager executable.

.NOTES
Use this function to initialize your script and to call GUI forms.

.NOTES
To get the console output in the Packager (Forms Engine) use:
$ConsoleOutput (Type: System.Collections.ArrayList)
#>
Param ([String]$Commandline)

#--------------------------------------------------------------------------
#TODO: Add initialization script here (Load modules and check requirements)
If (!(Test-Path "$env:USERPROFILE\Documents\WindowsPowershell\Modules\gShell"))
{
New-Item -Path "$env:USERPROFILE\Documents\WindowsPowershell\Modules" -ItemType "Directory" -Name "gShell"
Install-Gshell
$gShellzip = Join-Path ("$env:ProgramFiles\Application\Active Directory Tool") 'gShell.zip'
Expand-Archive -literalPath $gShellzip -DestinationPath "$env:USERPROFILE\Documents\WindowsPowershell\Modules"
Start-Sleep 3
$gShellpath = "C:\users\$env:username\documents\WindowsPowershell\Modules\gShell\gShellSettings.config"
(Get-Content $gShellpath).replace("customuser", $env:username) | Set-Content $gShellpath
}
Import-Module "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\gShell\gShell.dll"

#--------------------------------------------------------------------------

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Startup.pss seems to only run the first time

Post by davidc »

I don't see any obvious problems, but I have a few questions:

1. Do you get an any error messages?
2. What engine are you using when you package the script?
3. Are you using 64 Bit vs 32 Bit? The ProgramFiles folder is mapped differently for 32 bit vs 64 bit applications.
4. Do you need elevation?
5. Are you including the config file with the executable?
David
SAPIEN Technologies, Inc.

User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Re: Startup.pss seems to only run the first time

Post by falconcurt »

The weird part is that if i go to a new machine where i haven't ran the executable before it will work, but if i attempt to run it again even after i remove the application and made sure that the directories are no longer there. It doesn't run my Install-gShell function. It completely skips my If statement within the Main function.

1. No error messages
2. V5 host (Windows Forms)
3. 64 bit
4. Manifest Creation set to without manifest. Alternate Credentials set as Administrator, RunAs user.
5. I am including the config file

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Startup.pss seems to only run the first time

Post by davidc »

The Window Forms engine suppresses all output. I recommend trying the Windows engine or the Command line engine so that you see output messages.
Alternatively, you can place a Try Catch statement and use a MessageBox (msgbox snippet shortcut) to display any exceptions.
David
SAPIEN Technologies, Inc.

User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Re: Startup.pss seems to only run the first time

Post by falconcurt »

You lead me down the right track. I see what the problem was. The variable $env:userprofile and $env:username are referring to the RunAs user. Not the user i am logged into as. The test-path was checking c:\users\administrator instead of C:\users\"smithj".

DOH!