Created
January 10, 2019 18:02
-
-
Save Trucido/c573e64461b6820d547994e3a02dee20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set-StrictMode -Version 'Latest' | |
[bool]$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( | |
[Security.Principal.WindowsBuiltInRole]::Administrator) | |
[ScriptBlock]$script = {Get-Volume | Select-Object -ExpandProperty DriveLetter | Foreach-Object{Flush-Volume -DriveLetter $_}} | |
if ($IsAdmin) { | |
if (Get-Command 'Flush-Volume','Write-VolumeCache' -EA:4) { | |
& {$script} | |
} else { | |
throw 'Error' | |
} | |
} else { | |
[HashTable]$startProcessArgs = @{ | |
FilePath = "PowerShell.exe" | |
ArgumentList = "-Command", "& {$script}" | |
Verb = "runas" | |
WindowStyle = "Hidden" | |
PassThru = $false | |
Wait = $true | |
} | |
Write-Debug " Starting elevated shell with scriptblock: {$script}" | |
Write-Debug " Invoking Start-Process with args: $($startProcessArgs | Format-List | Out-String)" | |
Start-Process @startProcessArgs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment