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
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// A command to determine if the current PowerShell session is running with elevated (administrator) privileges.
/// </summary>
[Cmdlet(VerbsDiagnostic.Test, "Elevated")]
[OutputType(typeof(bool))]
public class TestElevatedCommand : PSCmdlet
{
/// <summary>
/// Tests if the current session is running with elevated privileges.
/// </summary>
protected override void EndProcessing()
{
WriteObject(Platform.IsElevated);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ CmdletsToExport=@("Add-Content",
"Set-ItemProperty",
"Get-TimeZone",
"Stop-Computer",
"Restart-Computer")
"Restart-Computer",
"Test-Elevated")
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ CmdletsToExport=@("Add-Content",
"Get-TimeZone",
"Set-TimeZone",
"Get-HotFix",
"Clear-RecycleBin")
"Clear-RecycleBin",
"Test-Elevated")
}
12 changes: 12 additions & 0 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.Management.Automation.Internal;
using Microsoft.Win32;
using System.Reflection.Metadata;

namespace System.Management.Automation
{
Expand Down Expand Up @@ -59,6 +60,17 @@ public static bool IsCoreCLR
}
}

/// <summary>
/// True if the current session is running with elevated (administrator) privileges.
/// </summary>
public static bool IsElevated
{
get
{
return Environment.IsPrivilegedProcess;
}
}

/// <summary>
/// True if the underlying system is NanoServer.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4653,6 +4653,12 @@ static InitialSessionState()
Platform.IsCoreCLR,
string.Empty,
ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope),

new SessionStateVariableEntry(
SpecialVariables.IsElevated,
Platform.IsElevated,
string.Empty,
ScopedItemOptions.Constant | ScopedItemOptions.AllScope),
#endregion
};

Expand Down
4 changes: 4 additions & 0 deletions src/System.Management.Automation/engine/SpecialVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ internal static class SpecialVariables

internal static readonly VariablePath IsCoreCLRPath = new VariablePath("IsCoreCLR");

internal const string IsElevated = "IsElevated";

internal static readonly VariablePath IsElevatedPath = new VariablePath("IsElevated");

#endregion

#region Preference Variables
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Test-Elevated cmdlet and $IsElevated variable' -Tags "CI" {
BeforeAll {
# Save the expected value
$expectedElevated = [System.Environment]::IsPrivilegedProcess
}

Context '$IsElevated automatic variable' {
It 'Should exist' {
{ Get-Variable IsElevated -ErrorAction Stop } | Should -Not -Throw
}

It 'Should be a boolean' {
$IsElevated | Should -BeOfType [bool]
}

It 'Should be constant' {
$var = Get-Variable IsElevated
$var.Options | Should -Match 'Constant'
}

It 'Should be AllScope' {
$var = Get-Variable IsElevated
$var.Options | Should -Match 'AllScope'
}

It 'Should have the correct value' {
$IsElevated | Should -Be $expectedElevated
}

It 'Should not be modifiable' {
{ Set-Variable IsElevated -Value $false -Force -ErrorAction Stop } | Should -Throw
}
}

Context 'Test-Elevated cmdlet' {
It 'Should exist' {
{ Get-Command Test-Elevated -ErrorAction Stop } | Should -Not -Throw
}

It 'Should return a boolean' {
$result = Test-Elevated
$result | Should -BeOfType [bool]
}

It 'Should match $IsElevated variable' {
$result = Test-Elevated
$result | Should -Be $IsElevated
}

It 'Should have correct output type' {
$cmd = Get-Command Test-Elevated
$cmd.OutputType.Type.Name | Should -Be 'Boolean'
}

It 'Should work without parameters' {
{ Test-Elevated } | Should -Not -Throw
}

It 'Should have correct verb' {
$cmd = Get-Command Test-Elevated
$cmd.Verb | Should -Be 'Test'
}

It 'Should have correct noun' {
$cmd = Get-Command Test-Elevated
$cmd.Noun | Should -Be 'Elevated'
}
}

Context 'Integration tests' {
It 'Both should return the same value' {
$cmdletResult = Test-Elevated
$variableValue = $IsElevated
$cmdletResult | Should -Be $variableValue
}

It 'Both should match the expected value' {
$cmdletResult = Test-Elevated
$variableValue = $IsElevated
$cmdletResult | Should -Be $expectedElevated
$variableValue | Should -Be $expectedElevated
}
}
}
1 change: 1 addition & 0 deletions test/powershell/engine/Basic/DefaultCommands.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ Describe "Verify aliases and cmdlets" -Tags "CI" {
"Cmdlet", "Tee-Object", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Test-Connection", "", $( $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Test-ComputerSecureChannel", "", $($FullCLR ), "", "", ""
"Cmdlet", "Test-Elevated", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Test-FileCatalog", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Test-Json", "", $( $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Test-ModuleManifest", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
Expand Down
Loading