Skip to content
Open
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 @@ -52,7 +52,11 @@ internal enum MatchMode
/// <summary>
/// Select the processes specified as input.
/// </summary>
ByInput
ByInput,
/// <summary>
/// Selects the current process.
/// </summary>
ByCurrent
}
/// <summary>
/// The current process selection mode.
Expand Down Expand Up @@ -108,13 +112,19 @@ public virtual Process[] InputObject
/// <summary>
/// Retrieve the list of all processes matching the Name, Id
/// and InputObject parameters, sorted by Id.
/// If the Current parameter is used only the current
/// process is returned.
/// </summary>
/// <returns></returns>
internal List<Process> MatchingProcesses()
{
_matchingProcesses.Clear();
switch (myMode)
{
case MatchMode.ByCurrent:
AddIdempotent(Process.GetCurrentProcess());
break;

case MatchMode.ById:
RetrieveMatchingProcessesById();
break;
Expand Down Expand Up @@ -448,6 +458,8 @@ public sealed class GetProcessCommand : ProcessBaseCommand
private const string NameWithUserNameParameterSet = "NameWithUserName";
private const string IdWithUserNameParameterSet = "IdWithUserName";
private const string InputObjectWithUserNameParameterSet = "InputObjectWithUserName";
private const string CurrentWithUserNameParameterSet = "CurrentWithUserName";
private const string CurrentParameterSet = "Current";

#endregion ParameterSetStrings

Expand Down Expand Up @@ -512,12 +524,34 @@ public override Process[] InputObject
}
}

/// <summary>
/// Get the Current Process
/// </summary>
[Parameter(ParameterSetName = CurrentParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = CurrentWithUserNameParameterSet, Mandatory = true)]
public SwitchParameter Current
{
get
{
return _current;
}

set
{
_current = value;
myMode = MatchMode.ByCurrent;
}
}

private SwitchParameter _current;

/// <summary>
/// Include the UserName.
/// </summary>
[Parameter(ParameterSetName = NameWithUserNameParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = IdWithUserNameParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = InputObjectWithUserNameParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = CurrentWithUserNameParameterSet, Mandatory = true)]
public SwitchParameter IncludeUserName { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@ Describe "Get-Process" -Tags "CI" {

$actual | Should -Be $expected
}

It "Should return Current Process with -Current" {
$currentProcess = Get-Process -Current
$currentProcess | Should -HaveCount 1
$currentProcess[0].Id | Should -BeExactly $PID
}

It "Should support -Current in RestrictedLanguage" {
$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
try {
$runspace.Open()
$runspace.SessionStateProxy.LanguageMode = [System.Management.Automation.PSLanguageMode]::RestrictedLanguage
$ps = [System.Management.Automation.PowerShell]::Create()
try {
$ps.Runspace = $runspace
[void]$ps.AddScript('Get-Process -Current | Select-Object -ExpandProperty Id')
$result = $ps.Invoke()
$ps.HadErrors | Should -BeFalse
$result | Should -HaveCount 1
$result[0] | Should -BeExactly $PID
}
finally {
if ($ps) {
$ps.Dispose()
}
}
}
finally {
if ($runspace) {
$runspace.Close()
$runspace.Dispose()
}
}
}
}

Describe "Get-Process Formatting" -Tags "Feature" {
Expand Down
Loading