-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Check if Elevated #26761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kilasuit
wants to merge
13
commits into
PowerShell:master
Choose a base branch
from
kilasuit:elevated
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Check if Elevated #26761
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
40893fb
Add Test-Elevated cmdlet and $IsElevated automatic variable implement…
Copilot fdda41d
Remove placeholder HelpUri from Test-Elevated cmdlet
Copilot 4f0e6c3
remove using statement
kilasuit fe754c5
make use of system.environment.IsPrivilegedProcess property
kilasuit 01fbb28
Simplified further
kilasuit 6affb40
remove unneeded _isElevated bool
kilasuit ae4f39e
make variable a constant as this should not be overwritten
kilasuit 9d7fc25
add Test-Elevated to Microsoft.PowerShell.Management.psd1's CmdletsTo…
kilasuit 24160d2
fix test
kilasuit 5a85ea1
fix default commands test
kilasuit b7c980f
Fix confirm impact
kilasuit 3442d2b
Fix test
kilasuit 36ff458
remove GetEuid.cs as not used anywhere
kilasuit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/Microsoft.PowerShell.Commands.Management/commands/management/TestElevatedCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Elevated.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.