forked from krzydoug/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-CmdAsSystem.ps1
More file actions
29 lines (23 loc) · 1.32 KB
/
Invoke-CmdAsSystem.ps1
File metadata and controls
29 lines (23 loc) · 1.32 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
26
27
28
29
Function Invoke-CmdAsSystem {
[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'
)
Write-Verbose "Verifying process is elevated"
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$iselevated = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if($iselevated -ne $true){
Write-Warning "Invoke-CmdAsSystem must be ran as administrator"
return
}
$psexec = Join-Path $env:TEMP 'Psexec.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','cmd.exe','/accepteula','/nobanner' -WindowStyle Hidden 2>&1
}