forked from krzydoug/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-PowershellAsSystem.ps1
More file actions
25 lines (20 loc) · 1.03 KB
/
Invoke-PowershellAsSystem.ps1
File metadata and controls
25 lines (20 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function Invoke-PowershellAsSystem {
[CmdletBinding()]
param (
[parameter(position=0)]
[Validateset('Desktop','Core')]
[validatescript({if($_ -eq 'core' -and -not (Get-ChildItem HKLM:\Software\Microsoft\PowerShellCore\InstalledVersions |Where-Object{(Get-ItemProperty $_.pspath) -match '(6|7)\.\d+\.\d+'})){throw "Powershell core is not detected"}else{$true}})]
$Edition = 'Desktop'
)
$psexec = Join-Path $env:TEMP 'Psexec.exe'
$exe = @{
Desktop = 'Powershell.exe'
Core = 'Pwsh.exe'
}
if(-not (Test-Path $psexec)){
Write-Verbose "Downloading psexec.exe from live.sysinternals.com"
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest https://live.sysinternals.com/tools/psexec.exe -OutFile $psexec -UseBasicParsing
}
Start-Process $psexec -ArgumentList '/s','/i',$exe[$Edition],'/accepteula','/nobanner' -WindowStyle Hidden -Verb runas 2>&1
}