Skip to content

Instantly share code, notes, and snippets.

@Krusen
Created November 15, 2017 20:35
Show Gist options
  • Select an option

  • Save Krusen/109943846d73daf2917c2fd7850df44e to your computer and use it in GitHub Desktop.

Select an option

Save Krusen/109943846d73daf2917c2fd7850df44e to your computer and use it in GitHub Desktop.
PowerShell - Enable running the script on a network drive
# Run as Administrator
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
# Load mapped network drives and replace with actual UNC path
$MappedDrives = @{}; Get-WmiObject win32_logicaldisk -Filter 'drivetype=4' | Foreach { $MappedDrives.($_.deviceID) = $_.ProviderName }
$networkFile = Get-Item $PSCommandPath | Select @{n='UncPath'; e= { $_.FullName.Replace( $_.Directory.Root, $MappedDrives.($_.Directory.Root.ToString().SubString(0,2)) +'\') }}
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$($networkFile.UncPath)`"" -Verb RunAs
exit
}
Set-Location $PSScriptRoot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment