Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 54ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
p

powershell

Recently created Least recently created Recently updated Least recently updated
wthiel

wthiel / Elevated Powershell Example

Last active 4 months ago powershell

This script ensures that it runs with administrator privileges before executing the actual code. If no admin rights are available, it restarts itself with elevated privileges.

0 0 1
1 param([switch]$Elevated)
2
3 function Test-Admin {
4 $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
5 $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
6 }
7
8 if ((Test-Admin) -eq $false) {
9 if ($elevated) {
10 # tried to elevate, did not work, aborting