Skip to content

Instantly share code, notes, and snippets.

@perXautomatik
Forked from willsantos/mount-vhd.ps1
Last active October 6, 2022 14:32
Show Gist options
  • Save perXautomatik/7814bbcb051a3a77c635fbe3c3d0dfc8 to your computer and use it in GitHub Desktop.
Save perXautomatik/7814bbcb051a3a77c635fbe3c3d0dfc8 to your computer and use it in GitHub Desktop.
Mount VHD
if ((get-command Get-VM -erroraction silentlycontinue) -ne $null)
{Get-VM *}
$importance = "Failed", "Warning", "Success"
$list = @(
@{ name = "Warning" }
@{ name = "Success" }
@{ name = "Failed" }
)
Get-WmiObject Win32_Volume | Format-Table Name, Label, FreeSpace, Capacity
Get-WmiObject -Class Win32_PnPEntity | ?{$_.name -eq 'Microsoft Virtual Disk'} | Format-Table Name, Label, PNPDeviceID
$alreadyMounted = (Get-Volume | Get-DiskImage).ImagePath
echo "--------------- already mounted ----------------"
$alreadyMounted
$toMount = (@(Get-ChildItem -path '\\100.84.7.151\NetBackup\VirtualDrive' -filter *vhdx -erroraction silentlycontinue).fullname +
@(Get-ChildItem -path '\\192.168.0.166\NetBackup\VirtualDrive' -filter *vhdx -erroraction silentlycontinue ).fullname ) | ?{ $_ -notin $alreadyMounted }
echo "--------------- toMount ----------------"
$toMount |%{ [System.IO.FileInfo]$_ | select name }
[cmdletbinding()]
param(
[parameter(
Mandatory = $true,
ValueFromPipeline = $true)]
[string]$path,
[parameter(
Mandatory = $false)]
[switch]$Elevated
)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated)
{
echo "ERROR! can't mount unless elevated to admin"
}
else {
Start-Process pwsh.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
Write-Output 'running with full privileges' "Montando VHD...."
$ErrorActionPreference = "silently"
if ((get-command mount-vhd -erroraction silentlycontinue) -eq $null)
{Mount-DiskImage $path}
else {
$disk_number = (Mount-VHD -Path $path -PassThru | Get-Disk).Number
Write-Output "VHD montada com sucesso"
}
param($path)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
$ArgumentList = "-noprofile -noexit -file `"{0}`" -elevated"
Start-Process pwsh -Verb RunAs -ArgumentList ($ArgumentList -f ($myinvocation.MyCommand.Definition)) -Wait
Exit
}
write-host "in admin mode.."
echo "initiating";
$toMount = Get-ChildItem -Path $path -Filter *.vhdx
$res | Sort-Object { $importance.IndexOf($_.Result) } |
% -Begin{$i = 1} -Process {
Write-Output 'running with full privileges' "Montando VHD...."
$path = $_.FullName
$ErrorActionPreference = "silently"
if ((get-command mount-vhd -erroraction silentlycontinue) -eq $null)
{Mount-DiskImage $path}
else {
$disk_number = (Mount-VHD -Path $path -PassThru | Get-Disk).Number
Write-Output "VHD montada com sucesso"
}
; $i++
$Completed = ($i/$toMount.count*100)
Write-Progress -Activity "trying to mount" -Status "Progress:" -PercentComplete $Completed
Start-Sleep -Milliseconds 250
}
;
Start-Sleep -Milliseconds 2500
#Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope CurrentUser -Force
function is-Admin {
# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$currentPrincipal.IsInRole($adminRole)
}
# Check to see if we are currently running as an administrator
if (is-Admin)
{
# We are running as an administrator, so change the title and background colour to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
$Host.UI.RawUI.BackgroundColor = "DarkBlue";
#Clear-Host;
}
else {
# We are NOT running as an administrator, so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";
$Invocation = (Get-Variable MyInvocation ).Value
# Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
$newProcess.Arguments = "& '" + $Invocation.MyCommand.Path + "'"
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
echo "Exit from the current, unelevated, process"
#Exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment