Get-Process: Remove admin requirement for -IncludeUserName (#21302)

This commit is contained in:
Jordan Borean authored and GitHub committed 2024-03-04 21:48:19 -08:00
1 parent aa97c515c8
commit 4e2682920a
3 files changed
+11 -47

No files matched your search

@@ -544,20 +544,6 @@ namespace Microsoft.PowerShell.Commands
#region Overrides
/// <summary>
/// Check the elevation mode if IncludeUserName is specified.
/// </summary>
protected override void BeginProcessing()
{
// The parameter 'IncludeUserName' requires administrator privilege
if (IncludeUserName.IsPresent && !Utils.IsAdministrator())
{
var ex = new InvalidOperationException(ProcessResources.IncludeUserNameRequiresElevation);
var er = new ErrorRecord(ex, "IncludeUserNameRequiresElevation", ErrorCategory.InvalidOperation, null);
ThrowTerminatingError(er);
}
}
/// <summary>
/// Write the process objects.
/// </summary>
@@ -685,7 +671,7 @@ namespace Microsoft.PowerShell.Commands
}
else
{
WriteObject(IncludeUserName.IsPresent ? AddUserNameToProcess(process) : (object)process);
WriteObject(IncludeUserName.IsPresent ? AddUserNameToProcess(process) : process);
}
}
}
@@ -765,28 +751,17 @@ namespace Microsoft.PowerShell.Commands
}
var tokenUser = Marshal.PtrToStructure<Win32Native.TOKEN_USER>(tokenUserInfo);
// Max username is defined as UNLEN = 256 in lmcons.h
// Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h
// The buffer length must be +1, last position is for a null string terminator.
int userNameLength = 257;
int domainNameLength = 16;
Span<char> userNameStr = stackalloc char[userNameLength];
Span<char> domainNameStr = stackalloc char[domainNameLength];
Win32Native.SID_NAME_USE accountType;
// userNameLength and domainNameLength will be set to actual lengths.
if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType))
{
return null;
}
userName = string.Concat(domainNameStr.Slice(0, domainNameLength), "\\", userNameStr.Slice(0, userNameLength));
SecurityIdentifier sid = new SecurityIdentifier(tokenUser.User.Sid);
userName = sid.Translate(typeof(System.Security.Principal.NTAccount)).Value;
}
catch (NotSupportedException)
{
// The Process not started yet, or it's a process from a remote machine.
}
catch (IdentityNotMappedException)
{
// SID cannot be mapped to a user
}
catch (InvalidOperationException)
{
// The Process has exited, Process.Handle will raise this exception.
@@ -898,7 +873,7 @@ namespace Microsoft.PowerShell.Commands
_timeOutSpecified = true;
}
}
/// <summary>
/// Gets or sets a value indicating whether to return after any one process exits.
/// </summary>
@@ -1047,7 +1022,7 @@ namespace Microsoft.PowerShell.Commands
}
}
}
if (PassThru)
{
WriteObject(_processList, enumerateCollection: true);
@@ -204,9 +204,6 @@
<data name="ContradictParametersSpecified" xml:space="preserve">
<value>Parameters "{0}" and "{1}" cannot be specified at the same time.</value>
</data>
<data name="IncludeUserNameRequiresElevation" xml:space="preserve">
<value>The 'IncludeUserName' parameter requires elevated user rights. Try running the command again in a session that has been opened with elevated user rights (that is, Run as Administrator).</value>
</data>
<data name="CouldNotDebugProcess" xml:space="preserve">
<value>Cannot debug process "{0} ({1})" because of the following error: {2}</value>
</data>
@@ -1,10 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') {
It "Should support -IncludeUserName" {
(Get-Process -Id $PID -IncludeUserName).UserName | Should -Match $env:USERNAME
}
It "Should support -Module" -Pending:$IsMacOS {
$modules = Get-Process -Id $PID -Module
$modules.GetType() | Should -BeExactly "System.Object[]"
@@ -78,12 +74,8 @@ Describe "Get-Process" -Tags "CI" {
(Get-Process -Id $PID).Id | Should -BeExactly $PID
}
It "Should fail to run Get-Process with -IncludeUserName without admin" -Skip:(!$IsWindows) {
if (Test-IsElevated) {
Set-ItResult -Skipped -Because "must NOT be run as admin"
}
{ Get-Process -IncludeUserName } | Should -Throw -ErrorId "IncludeUserNameRequiresElevation,Microsoft.PowerShell.Commands.GetProcessCommand"
It "Should support -IncludeUserName" {
(Get-Process -Id $PID -IncludeUserName).UserName | Should -Match $env:USERNAME
}
It "Should fail to run Get-Process with -Module without admin" -Skip:(!$IsWindows) {