Skip to content

Instantly share code, notes, and snippets.

@David-Lor
Created October 28, 2018 07:35
Show Gist options
  • Save David-Lor/58b3cbe2385a4852658275eb217bb9e7 to your computer and use it in GitHub Desktop.
Save David-Lor/58b3cbe2385a4852658275eb217bb9e7 to your computer and use it in GitHub Desktop.
Mount several Virtual Hard Disks in Windows programmatically
python "F:\Juegos\DiscosVirtuales\Montar.py"
import os
import subprocess
import threading
DIR = "F:\Juegos\DiscosVirtuales"
EXTENSIONS = ["vhd", "vhdx"]
BAT_LOCATION = "R:\\"
disks = [os.path.join(DIR, d) for d in os.listdir(DIR) if d.split(".")[-1] in EXTENSIONS]
threads = list()
def threaded_f(index, disk):
filename0 = os.path.join(BAT_LOCATION, f"diskpart_{index}_A.bat")
filename1 = os.path.join(BAT_LOCATION, f"diskpart_{index}_B.bat")
with open(filename0, "w") as f:
f.write(f"diskpart /s {filename1}")
with open(filename1, "w") as f:
f.write(f'select vdisk file="{disk}"\nattach vdisk')
prog = subprocess.Popen(['runas', '/user:Administrator', '/savedcred', filename0], stdin=subprocess.PIPE)
for index, disk in enumerate(disks):
th = threading.Thread(target=threaded_f, args=(index, disk))
threads.append(th)
th.start()
for th in threads:
th.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment