Skip to content

Commit 91cf4ad

Browse files
committed
Simplify the username obtainment within ProcessUtilities.GetRunningProcesses().
1 parent 21a75f4 commit 91cf4ad

14 files changed

Lines changed: 4 additions & 19 deletions

src/PSADT/PSADT/ProcessManagement/ProcessUtilities.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Security.Principal;
1010
using System.ServiceProcess;
1111
using Microsoft.Win32.SafeHandles;
12-
using PSADT.AccountManagement;
1312
using PSADT.Extensions;
1413
using PSADT.FileSystem;
1514
using PSADT.LibraryInterfaces;
@@ -182,8 +181,8 @@ static string[] GetProcessArgv(Process process, Dictionary<Process, string[]> pr
182181
NTAccount? username = null;
183182
if (!process.HasExited)
184183
{
185-
// Leverage SeDebugPrivilege if we have it for complete accuracy.
186-
if (PrivilegeManager.HasPrivilege(SE_PRIVILEGE.SeDebugPrivilege))
184+
// Users can only get the username for their own processes, whereas admins can get anyone's.
185+
try
187186
{
188187
// We're caching the process, so don't dispose of its SafeHande as .NET caches it also...
189188
_ = AdvApi32.OpenProcessToken(process.SafeHandle, TOKEN_ACCESS_MASK.TOKEN_QUERY, out SafeFileHandle hToken);
@@ -192,23 +191,9 @@ static string[] GetProcessArgv(Process process, Dictionary<Process, string[]> pr
192191
username = TokenManager.GetTokenSid(hToken).Translate(typeof(NTAccount)) as NTAccount;
193192
}
194193
}
195-
196-
// If we couldn't get it that way, test for session Id equality.
197-
if (username is null)
194+
catch (Exception ex) when (ex.Message is not null)
198195
{
199-
// This needs to be in a try/catch as accessing SessionId can throw E_ACCESSDENIED exceptions.
200-
try
201-
{
202-
if (process.SessionId == AccountUtilities.CallerSessionId)
203-
{
204-
// The process is in our session, so assume it's ours.
205-
username = AccountUtilities.CallerUsername;
206-
}
207-
}
208-
catch (Exception ex) when (ex.Message is not null)
209-
{
210-
username = null;
211-
}
196+
username = null;
212197
}
213198
}
214199

Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)