Created
November 15, 2017 20:35
-
-
Save Krusen/109943846d73daf2917c2fd7850df44e to your computer and use it in GitHub Desktop.
PowerShell - Enable running the script on a network drive
This file contains hidden or 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
| # 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