Run a PSscript with other credentials, capture the output/debug as admin?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

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 3 years and 11 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.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Run a PSscript with other credentials, capture the output/debug as admin?

Post by stevens »

Hi,

I have a powershell script that I run from a share and which is ran by another user.
It run
Start-Process powershell.exe -ArgumentList -NoProfile -WindowStyle Normal -executionpolicy bypass -noninteractive \\pathtomyscript\script -optionsofscript -credential AdminCredentials

That works fine.
However the powershell window opens then closes, success or failure.

I'd like to keep the Powershell window open when an issue occurs (or even if not) so I can debug but for a normal user it should also show some output "ok or not ok" maybe also the error would be nice (the script runs under admin credentials so a normal user should not have the option to enter stuff in the powershell).

How can I achieve this?
Please advise.
Thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run a PSscript with other credentials, capture the output/debug as admin?

Post by jvierra »

Your arguments are out of order. YOU cannot use named arguments on a command line. THe arguments must be last on the commandline.

Start by reading the help carefully until you understand what is telling you.

help about_PowerShell.exe
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Run a PSscript with other credentials, capture the output/debug as admin?

Post by stevens »

Thanks, I will certainly look at it.
But my command is working as is, so would first like to make it work in the GUI, then adapt/finetune.
It works outside the GUI, but within the GUI I get this as a result :-(

>> Start-Process powershell.exe -ArgumentList "$ToExecute" -Credential $AdminCredentials -PassThru -Wait

Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
0 0 0 0,52 4984
User avatar
Alexander Riedel
Posts: 8489
Last visit: Thu Apr 18, 2024 9:59 am
Answers: 20
Been upvoted: 37 times

Re: Run a PSscript with other credentials, capture the output/debug as admin?

Post by Alexander Riedel »

You'll need to show your actual code, not just the one line.
Show the function or event handler wherein you call that.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run a PSscript with other credentials, capture the output/debug as admin?

Post by jvierra »

(the script runs under admin credentials so a normal user should not have the option to enter stuff in the powershell).
If the PS remains running then there is really no way to prevent a user from typing into it an changing anything an admin can change.

Without some clear idea of what the task is there is really no oi way to advise you.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Run a PSscript with other credentials, capture the output/debug as admin?

Post by stevens »

Thanks for the input. I had a typo, will get back with details later on to finetune if needed.
This topic is 3 years and 11 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.
Locked