powershell:powershell_mit_erweiterten_rechten_im_script_starten
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
powershell:powershell_mit_erweiterten_rechten_im_script_starten [2017/03/30 13:24] – angelegt ronny | powershell:powershell_mit_erweiterten_rechten_im_script_starten [2017/03/30 14:00] (aktuell) – ronny | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== Powershell mit erweiterten Rechten im Script starten ====== | ====== Powershell mit erweiterten Rechten im Script starten ====== | ||
- | |||
- | [[https:// | ||
<code Powershell> | <code Powershell> | ||
- | # Get the ID and security principal of the current user account | + | $myScript = { |
+ | get-childitem >> c: | ||
+ | } | ||
+ | $newProcess = New-Object System.Diagnostics.ProcessStartInfo " | ||
+ | $newProcess.Arguments = $myScript | ||
+ | $newProcess.Verb = " | ||
+ | [System.Diagnostics.Process]:: | ||
+ | </ | ||
- | $myWindowsID=[System.Security.Principal.WindowsIdentity]:: | + | Dieses Script startet eine neue Powershell in einem separaten Prozess, die über ein volles Administrator-Token verfügt |
- | $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID) | + | Quelle: [[https:// |
- | + | {{tag>[Powershell Elevated]}} | |
- | + | ||
- | # Get the security principal for the Administrator role | + | |
- | + | ||
- | $adminRole=[System.Security.Principal.WindowsBuiltInRole]:: | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Check to see if we are currently running "as Administrator" | + | |
- | + | ||
- | if ($myWindowsPrincipal.IsInRole($adminRole)) | + | |
- | + | ||
- | { | + | |
- | + | ||
- | # We are running "as Administrator" | + | |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
- | + | ||
- | | + | |
- | + | ||
- | } | + | |
- | + | ||
- | else | + | |
- | + | ||
- | { | + | |
- | + | ||
- | # We are not running "as Administrator" | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Create a new process object that starts PowerShell | + | |
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Specify the current script path and name as a parameter | + | |
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Indicate that the process should be elevated | + | |
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Start the new process | + | |
- | + | ||
- | [System.Diagnostics.Process]:: | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Exit from the current, unelevated, process | + | |
- | + | ||
- | | + | |
- | + | ||
- | } | + | |
- | + | ||
- | + | ||
- | + | ||
- | # Run your code that needs to be elevated here | + | |
- | + | ||
- | Write-Host -NoNewLine "Press any key to continue..." | + | |
- | + | ||
- | $null = $Host.UI.RawUI.ReadKey(" | + | |
- | </ | + |
powershell/powershell_mit_erweiterten_rechten_im_script_starten.1490873098.txt.gz · Zuletzt geändert: 2017/03/30 13:24 von ronny