What is default script path for PSS scripts?

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.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

What is default script path for PSS scripts?

Post by shiroscout »

When using the forms and making the GUI...
I am trying to execute a powershell .ps1 file

I am confused about having the project folder with my Main form at root of that folder.
then I have sub-folders.

I'm trying to use this code:

Start-Process -FilePath "$PSScriptRoot\ImportPs1\WindowsUpdates.ps1"

But I have tried it as a Start-Job, Invoke-Item, Invoke-Command.

I have tired several variants of code from online, but I must obviously be doing something wrong.

With this particular command I am trying to get the .ps1 script to actually pop open a powershell windows or get minimal texbox output that the process is complete.

The .ps1 does windows updates with no user prompts and no interaction. It does a check, does the downloads and install them. It does give a summary at end when is completes.

I used a "Powershell Studio Project" and I thought the default root script path would be the directory of the project folder that all commands would run from?
Thank You,

Wayne
User avatar
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

Re: What is default script path for PSS scripts?

Post by shiroscout »

This command worked for launching a powershell window from within the Windows Form from a button click:

Code: Select all

invoke-expression 'cmd /c start powershell -NoExit -Command ".\\ImportPs1\AutoWinUpdates.ps1"'
I had to use a slightly different ps1 file for the Windows Updates and am working to find a way to say "Yes" to installing the updates, but it does launch, search, download the updates without user interaction.
At the end of the script it give an errorlevel code and reboot required True/False which I am trying to get into the textbox output and have the powershell window close on it's own, but glad I got this far with it.

Hope this helps someone else with similar need. :D
Thank You,

Wayne
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: What is default script path for PSS scripts?

Post by davidc »

If you want to get the folder of the project script, I recommend using the Get-ScriptDirectory function (snippet shortcut: getscriptdirectory). This function will work in package executable or normal script host.
David
SAPIEN Technologies, Inc.
User avatar
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

Re: What is default script path for PSS scripts?

Post by shiroscout »

David,

:lol: :shock: There is a built-in command? LOL I spent hours trying to get this one thing to work! LOL :mrgreen:
I'll try your post directions for using Get-ScriptDirectory.

I thought I needed to use the $PSScriptRoot. :roll: :mrgreen:
Yes, mostly, when needed, I want to be able to use the buttons, and if not running a PowerShell command or native windows program.
I want to start at location of root from project script ( Often and almost always )and point to different sub-folders that have contents: .PDF, .PS1, .VBS, .CMD , etc... files.
I'm not sure what the extra backslash in the line : ".\\ImportPs1\AutoWinUpdates.ps1" is actually referencing but it had worked and did open up a PowerShell Window. :D
Usually, mostly, I do no ever want a separate windows coming up but prefer for the user reference/focus to be within the GUI, but in a few scenarios I wanted it to be clear what was actually happening.
Obviously, my lack of skills :roll: :oops: is preventing a more elegant solution like a minimal little message window, or something build into the script exe outputbox ( I just kept getting no output, but also had to use a .ps1 file that needs a user response of Yes/No )would have been nicer. I'm just having to make due for some things I cannot yet figure out a better solution or find / tweak code / scripts to fit needs.
I do plan to keep form, and do updates, so over time the code should get cleaner, more soley PowerShell based, and ascetically more pleasing. :lol: :o I'm being a little easy on myself and not expecting perfection as this is my first ever major PowerShell / GUI attempt.
Thank You,

Wayne
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: What is default script path for PSS scripts?

Post by davidc »

Actually $PSScriptRoot should work. A few builds ago, we added $PSScriptRoot support to our package exectuables.

The .\ImportPs1\AutoWinUpdates.ps1 path will only work if the current directory the same as the script's directory. This might not always be the case depending on how the script as ran.

By calling PowerShell.exe you are running the script in a separate process (thus the popup console). If you want to invoke a script file, you just have to dot source it directly in PowerShell:

Code: Select all

. $PSScriptRoot\ImportPs1\AutoWinUpdates.ps1
David
SAPIEN Technologies, Inc.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.