Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -544,20 +544,6 @@ public override Process[] InputObject

#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>
Expand Down Expand Up @@ -685,7 +671,7 @@ protected override void ProcessRecord()
}
else
{
WriteObject(IncludeUserName.IsPresent ? AddUserNameToProcess(process) : (object)process);
WriteObject(IncludeUserName.IsPresent ? AddUserNameToProcess(process) : process);
}
}
}
Expand Down Expand Up @@ -765,28 +751,17 @@ private static string RetrieveProcessUserName(Process process)
}

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.
Expand Down Expand Up @@ -898,7 +873,7 @@ public int Timeout
_timeOutSpecified = true;
}
}

/// <summary>
/// Gets or sets a value indicating whether to return after any one process exits.
/// </summary>
Expand Down Expand Up @@ -1047,7 +1022,7 @@ protected override void EndProcessing()
}
}
}

if (PassThru)
{
WriteObject(_processList, enumerateCollection: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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[]"
Expand Down Expand Up @@ -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) {
Expand Down