Running a ps1 file as elevated

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 5 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
Dennishk
Posts: 3
Last visit: Mon May 11, 2020 10:59 pm

Running a ps1 file as elevated

Post by Dennishk »

Product, version and build:
64 bit - PowerShell Studio 2018 v5.5.152
Operating system:
64 bit OS Windows 10 EDU

Hi,

Enviroment:
My script/program is a GUI (.psf) build as an EXE for the supporters.

Problem:
I need to change a registry key and it entries. This is becuase i need to be able to run Explorer.exe as other user. I found something online that did the trick. Only problem is that my script is run as other user and not the local user as administrator on the machine.
So, i need my script to open a powershell that runs this script (Attachment1.ps1). I have tried to add the .ps1 file and call it like this - Start-Process powershell -ArgumentList 'EnablePrivilige.ps1' -verb RunAs -Wait - does not work. I have tried to open it what functions like this - Start-Process powershell EnablePrivilige -verb RunAs -Wait - Does not work either. The powershell window opens fine but does not run the script becuase it is unknown to that session.

Question:
How do i run a script from either a .ps1 fil from my project or as a function in a NEW powershell window?

Kind Regards
Dennis
Attachments
EnablePrivilige.ps1
(3.92 KiB) Downloaded 71 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Running a ps1 file as elevated

Post by davidc »

What error did you get?

Note: anytime you run a script as another user, you have to make sure that user has access to the folder where the script is located. For example, if you run the script from user specific folder such as My Documents, the Run As might not be able to access the file. So make sure you run the script in a non-user specific folder.
David
SAPIEN Technologies, Inc.
User avatar
Dennishk
Posts: 3
Last visit: Mon May 11, 2020 10:59 pm

Re: Running a ps1 file as elevated

Post by Dennishk »

Hi Davidc

Thanks for your reply.
I have posted a new picture in this comment.

As can be seen from the picture is what i am trying to do.
Picture:
Running a script file that is in the project. Not "anywhere" on any machine - This is becuase i build the project as an EXE file.
Right now the line in red (1) is running the script form my PC. BUT the script needs to be able to be called from the project (2) Red Line.
Or maybe export it for the duration it is needed.

!Remember - I am building it as an EXE so non of the files in the project can be seen by the Supporter that is using this.

Thanks in advance
Attachments
Sapien1.png
Sapien1.png (73.17 KiB) Viewed 1525 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Running a ps1 file as elevated

Post by davidc »

In your script, you are accessing a script file from disk. If that file is not located in the path you specified, the executable / PowerShell will fail to run it.

If you want to prevent the user from seeing the script, I recommend converting EnablePrivilige.ps1 into its own separate executable and invoke that instead of the ps1 file.
David
SAPIEN Technologies, Inc.
This topic is 5 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.