Detecting user that launches packaged script while using Run As

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Post Reply
User avatar
cdouglas_nch
Posts: 4
Joined: Tue May 10, 2016 10:12 am

Detecting user that launches packaged script while using Run As

Post by cdouglas_nch » Mon Dec 04, 2017 9:10 pm

OS: Windows 10 Build 1607 x64
PS Studio Build: Latest (not at work, but I always keep it up to date)

When packaging a script and configuring it to run as another user, does anyone know how to obtain the username of the user that launches the first process?

For example, if I have a packaged script called 'Script.exe' configured to run as 'UserB', and then launch 'Script.exe' as 'UserA', $env:username returns 'UserB' as I would expect (the same applies when using the impersonate option). I'm trying to figure out a way to identify 'UserA'.

So far I've identified that in a normal PowerShell prompt (both elevated and non-elevated) I can use the WMI class Win32_Process and method GetOwner to identify the the owner of the parent process of the PowerShell prompt. Problem is when I try this with a packaged script the methods return value is 2 (access denied). I've tried launching 'Script.exe' with 'UserA' configured as a local administrator, still get an access denied.

Does anyone know how to get the GetOwner method to work, or have another means of getting the username of the parent process? The end goal is to have Script.exe run as 'UserB' but write to a log file specifically for 'UserA'.

User avatar
Alexander Riedel
Posts: 6361
Joined: Tue May 29, 2007 4:43 pm

Re: Detecting user that launches packaged script while using Run As

Post by Alexander Riedel » Tue Dec 05, 2017 12:35 am

[Topic moved by moderator]
Please note that the Customer support forum is not intended for technical support or general Powershell questions. It is dedicated to assist customers with sales and licensing questions.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 11558
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Detecting user that launches packaged script while using Run As

Post by jvierra » Tue Dec 05, 2017 4:41 am

The only way to get another users process is to run as an elevated admin. Regular users cannot see the owner of processes other then ones they have started.

A process launched with alternate credentials runs in a new session and it is not a child of the process that started it.

Pass the username to the process on the command line when you launch it.

Post Reply