Task Scheduler – Mount VHD and Mark Online Read/Write Mode – Powershell

Some helpful code. I needed to mount a VHDX on System Startup with a Scheduled Task, and have it automatically mount the drive into a writable state, since by default,  Mount-VHD mounts in read-only mode, and with disk status offline.

Save the code into a .PS1 file, call it upon system startup.

Code below.

#######Mount a VHDX and mark as online writable via Powershell#######

##Mount the VHDX
Mount-VHD -Path "D:\Folder\VHD-Disk.vhdx"

#Obtain Details about the VHDX, look for the UniqueID Field
Get-Disk | FL

##Replace the UniqueID field below, set properties of default offline status, and default read-only status, to off/false.
##The -IsReadOnly command needs to be run separately from the -IsOffline command, or else you'll get the error, "Parameter set cannot be resolved using the specified named parameters."
##Drive will auto-map with the last drive-letter specified through Disk Management.
##If you want to specify a Partition Letter for newly created disks, you'll need to use DiskPart or another Partition-related tool, rather than a disk-specific tool like "Mount-VHD"
Get-Disk | Where-Object UniqueID -eq 12345678901234567891234567890123 | Set-Disk -IsOffline $False
Get-Disk | Where-Object UniqueID -eq 12345678901234567891234567890123 | Set-Disk -IsReadOnly $False

Create a Scheduled Task via Command Line to automount the drive on boot.

::Creates a Scheduled Task.
::TN = Task Name
::SC = Schedule/Time
::RU = Run User
::TR = Task Run
::Format for command is "'EXE' paramters"
schtasks /create /TN "AutoMount VHD" /SC ONSTART /RU "NT AUTHORITY\SYSTEM" /TR "'%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe' C:\scripts\AutoMountVHDX.ps1"

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *