Skip to content

Instantly share code, notes, and snippets.

@zett42
Last active February 9, 2023 13:23
Show Gist options
  • Save zett42/25bbe4b0d261efe1e1050c570b9191b6 to your computer and use it in GitHub Desktop.
Save zett42/25bbe4b0d261efe1e1050c570b9191b6 to your computer and use it in GitHub Desktop.
Self-Elevate PowerShell script
# PowerShell demo to self-elevate a script
# - Makes sure parameters are properly forwarded to the elevated script (preserving argument types and spaces in string arguments).
# - Passes the current directory to elevated script.
param(
[string] $Foo,
[int] $Bar
)
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$argsSerialized = [Management.Automation.PSSerializer]::Serialize( $PSBoundParameters )
$command = @"
Set-Location '$PWD'
`$theArgs = [Management.Automation.PSSerializer]::Deserialize(@'
$argsSerialized
'@)
& '$($MyInvocation.MyCommand.Path)' @theArgs
"@
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes( $command ))
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList "-EncodedCommand $encodedCommand"
Exit
}
}
"PWD: $pwd"
""
"Foo: $Foo [$($Foo.GetType().Name)]"
"Bar: $Bar [$($Bar.GetType().Name)]"
""
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment