Hi,
I have a form which performs quite a few actions, one of which requires elevation. I've set the script to run as an elevated user, but another of the functions is to print a particular file - obviously this tries to run the print job as the RunAs user.
What would be the best way of running code as admin and non-admin selectively? I've tried to run the required code in a scriptblock Job with credentials but it doesn't function properly.
Thanks
Elevate/De-elevate
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.
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.
Re: Elevate/De-elevate
It is not possible in Windows to run a process as two different users. YOU could use a standard process to start a scheduled job and retrieve the data from the job for printing.
- $opt = New-ScheduledJobOption -RunElevated
- $job = Register-ScheduledJob -Credentialuserid -RunNow -ScriptBlock {....} -ScheduledJobOption $opt
- $jobdata = $job | Wait-Job | Receive-Job
Re: Elevate/De-elevate
Thanks for the reply. It's actually the other way around... the compiled exe will be running elevated but I will need to print as the local user.
Re: Elevate/De-elevate
Why? Just print. You cannot execute an impersonated session as a normal user. You can allow a user to use other credentials to launch a session but that session cannot impersonate the launching user
Use a job to run the elevated session and retrieve the data for the user to print.
Use a job to run the elevated session and retrieve the data for the user to print.
Re: Elevate/De-elevate
If I print in the elevated EXE it runs the print dialog as the elevated user. The elevated user doesn't have that printer installed and we use print management to identify print jobs by user code so wouldn't work anyway.
- Alexander Riedel
- Posts: 8488
- Last visit: Tue Apr 16, 2024 8:42 am
- Been upvoted: 37 times
Re: Elevate/De-elevate
Elevation is per process and determined before the process is even started. You cannot change elevation during runtime.
Since any other process you will launch from the elevate process will be elevated as well, I would suggest a starter process, running as a regular user.
This launches the elevated process, which saves the data needing to be printed. Once the elevated process terminates, you starter process can load the file and print it.
This is of course assuming this is a sequential process and not event controlled.
Since any other process you will launch from the elevate process will be elevated as well, I would suggest a starter process, running as a regular user.
This launches the elevated process, which saves the data needing to be printed. Once the elevated process terminates, you starter process can load the file and print it.
This is of course assuming this is a sequential process and not event controlled.
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Elevate/De-elevate
If the elevated process launches a program that prints then print it to a file that the normal users session can submit to the printer. Just save the print file to a common location.