Skip to content

Instantly share code, notes, and snippets.

@Trucido
Created January 10, 2019 18:02
Show Gist options
  • Save Trucido/c573e64461b6820d547994e3a02dee20 to your computer and use it in GitHub Desktop.
Save Trucido/c573e64461b6820d547994e3a02dee20 to your computer and use it in GitHub Desktop.
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