Created
October 28, 2018 07:35
-
-
Save David-Lor/58b3cbe2385a4852658275eb217bb9e7 to your computer and use it in GitHub Desktop.
Mount several Virtual Hard Disks in Windows programmatically
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
python "F:\Juegos\DiscosVirtuales\Montar.py" |
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
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