Last week I showed you how I used PowerShell and diskpart to partition, format, and label 193 disks. If you’ll recall, I said I still needed to figure out how to get 188 of those disks mapped to folders for Volume Mount Points.
My goal was to get this 100% automated. Due to time constraints, I’m only at about 95%. This last bit takes a small amount of manual work. Don’t worry, it’s just simple copy and paste.
First we need to list out all volumes, to do this, open PowerShell, and run diskpart.
Next we just open Excel, put “Volume” in cell A1, and “Label” in cell B1. Copy “Volume ###” from the diskpart output and paste it into column A of the Excel sheet. Copy “Label” from the diskpart output and paste it into column B. Remove any disks that already have drive letters assigned. If you remember from last week, I had 5 disks that I assigned drive letters. After removing them from this list, I had 188 rows (not including the column names in row 1). Save it as a .csv file
Next we run a PowerShell script I called cvmp.ps1:
# Volume Mount Point Creation Script
# By Josh M. Bryant
# Last modified 9/6/2011 8:30 AM
$dmount = "E:EXCH10"
$lmount = "L:EXCH10"
$data = Import-CSV C:Scriptsvolumes.csv
ForEach ($line in $data)
$volume = $line.Volume
$label = $line.Label
Add-Content -path C:Scriptsvolumes.txt -value "Select $volume"
If ($label -like "*L*")
Add-Content -path C:Scriptsvolumes.txt -value "assign mount=$lmount$label"
Add-Content -path C:Scriptsvolumes.txt -value "assign mount=$dmount$label"
diskpart /s C:Scriptsvolumes.txt
That’s it! The PowerShell uses the .csv file to create the diskpart script that assigns the volume mount points.
I would eventually like to go back and script out the .csv file creation using the Export-CSV command. I was having trouble getting the output from the diskpart command (which is not a PowerShell command) into a usable format with the Export-CSV command.
Even with the manual copy and paste work, this saved a ton of time. Can you imagine how long all of this would have taken without scripting?
If you use scripts from this site, please keep the author info intact, and include a link back to this site if you post them anywhere else.
Josh M. Bryant is currently a Director of Technical Product Management at Tanium where he builds products that help customers overcome the challenges of managing very large scale computing environments. Prior to joining Tanium, he was a Premier Field Engineer at Microsoft specializing in Microsoft Exchange Server, and then later a Cybersecurity Architect specializing in Compromise Recovery.