Solved

Powershell script for Diskpart

Posted on 2011-09-08
16
1,410 Views
Last Modified: 2012-05-12
I have a CSV File that contains the Disk number and Lun Name of disk I need mounted to my servers (hundreds of servers)

I want a powershell script that will parse the csv files (unique per server) in to a text file that will contain a series of diskpart commands.

so for example here is my CSV
Disk Number,Lun Name   
Disk 002,D1_DB11_C1_data
Disk 003,D1_DB11_C1_log
Disk 004,D1_DB31_C2_data
Disk 005,D1_DB31_C2_log
Disk 006,D1_DB35_C1_data
Disk 007,D1_DB35_C1_log
Disk 008,D1_DB52_C2_data
Disk 009,D1_DB52_C2_log
Disk 010,D1_DB09_C2_data
Disk 011,D1_DB09_C2_log
Disk 012,D1_DB19_C1_data
Disk 013,D1_DB19_C1_log
Disk 014,D1_DB24_C2_data
Disk 015,D1_DB24_C2_log
Disk 016,D1_DB43_C1_data
Disk 017,D1_DB43_C1_log
Disk 018,D1_DB45_C2_data
Disk 019,D1_DB45_C2_log
Disk 020,Servername_restore
Disk 021,Servername_root_data
Disk 022,Servername_root_log
Disk 023,D1_DB02_C2_data
Disk 024,D1_DB02_C2_log
Disk 025,D1_DB03_C1_data
Disk 026,D1_DB03_C1_log
Disk 027,D1_DB27_C1_data
Disk 028,D1_DB27_C1_log
Disk 029,D1_DB38_C2_data
Disk 030,D1_DB38_C2_log
Disk 031,D1_DB51_C1_data
Disk 032,D1_DB51_C1_log

Open in new window


I need the scriptto go through and pick out the Root and restore disks and create them first and assing them to the D: L: R: drives.  

Then it will go through and pick out the remainder of the drives and format them,

The Script will need to parse the lun name and create folder names for the disk to mount to.

so the end result for example would be:

D:\ <Disk 021 mounted for D:>
D:\D1_DB11 <disk 002 mounted to this folder>
D:\D1_DB31 <Disk 004 mounted to this folder>
...

The Log drives are more complicated
L:\ <disk 022 mounted for L:>
L:\D1_Log11 <disk 003 mounted to this folder>
.....


I know all the diskpart commands, what I don't know is how to get powershell to work with the CSV File, and do the Root/Restore Drives First.

Also How do I parse the volume name and pull out portions of it for volume names, folder names, etc.

If it matters the LUN name predeedign either  _log or _data will aways be the same characther length as well as the server name.
0
Comment
Question by:brittonv
  • 9
  • 7
16 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 36506911
I'm not sure I'm getting what you are after. Are restore, root_data and root_log appearing only once per file?
What relevance has the server name?
What about the C1 and C2 part?

Assuming each of the root and restore entries are unique, and you know the diskpart commands already, here is a simple approach, just to show the figure:
$disks = Import-Csv "c:\temp\ee\diskpart.txt" -header DiskNo, Lun
$disks | ? {$_.Lun -like "*root_data"} {<# diskpart D: $_.DiskNo#>}
$disks | ? {$_.Lun -like "*root_log" } {<# diskpart L: $_.DiskNo#>}
$disks | ? {$_.Lun -like "*_restore" } {<# diskpart R: $_.DiskNo#>}
$disks | ? {$_.Lun -like "*_*_*_data"} {<# diskpart mount ... #>}
$disks | ? {$_.Lun -like "*_*_*_log" } {<# diskpart mount ... #>}

Open in new window

0
 
LVL 8

Author Comment

by:brittonv
ID: 36510109
yes the roots and restore are once per file.

The C1, C2 Parts are not part of the Folder/Mount point names.

Thanks
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36510279
To get the parts of the name, you can use (shown for line 6 of my skeleton above):
$disks | ? {$_.Lun -like "*_*_*_log" } {<# diskpart mount ... ($_ .Split("_")[0..1] -join "_")  #>}

Open in new window

That is, your mount point name is constructed by the first and second part, separated by an underscore. We could also say to use only the first 6 characters:
$disks | ? {$_.Lun -like "*_*_*_log" } {<# diskpart mount ... ($_[0..5] -join "") #>}

Open in new window

0
 
LVL 8

Author Comment

by:brittonv
ID: 36510859
Sorry I dont' know thie $disk | ? .... Format...

I am looking to create text file that I can feed to diskpart.

Can you give me an example on how to have what your saying export to a txt file?

so that the Root drives are first
then the moutn points.

Am I correct in saying in how you have is structured here, it processes all entries in teh CSV are processed by the first line.  Then they are all processed again by the second line .... to the end?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36511295
You are not correct. That is why I used separate lines for each case. In detail:
First line reads the complete CSV contents into a var, building an array of two strings "DiskNo" and "LUN".
Second line processes the objects in the var, one by one. The ?... is the shortcut/alias for "where-object", and acts as a filter - only get those records for which the condition is true. In this case I ask for the LUN having "root_data" at the end. There should be only one record as a result.
The following lines do the same for the log root, then restore, and last data and log.
The sequence is hence: first create the root data, then root log, then restore, then other.

I've not the time ATM to look for the precise diskpart commands, so could you provide an example for each case?
0
 
LVL 8

Author Comment

by:brittonv
ID: 36511389
Sure for the Root_data drive it would be (may not be perfect but ti will give me what I need.  Comments in ():

select disk 021 (from the CSV)
online disk
attribute disk clear readonly
create partition primary
format fs=ntfs Label=$LUN (the name as is from the csv)
Assign Letter=D

(for a drive mounted to a point point I would first have to create the directory)
mkdir d:\d1_db11 (name parsed from the Disk 002 Full Name of D1_db11_c1_data
select disk 002 (from CSV)
online disk
attribute disk clear readonly
create partition primary
format fs=ntfs Label=$LUN (the name as is from the csv)
assign mount=D:\db1_db11

Then for the Log drives I need to parce the mountpoints a little farther as I can't have the db11 in the name rather it will need to be L:\db1_Log11 for disk 003
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36511808
Hmm, that makedir part is difficult because you cannot do that in diskpart, and you can't do it before the root commands for diskpart are executed. We would have to create e.g. a batch file containing the diskpart and all other commands. It is much easier if we can just execute diskpart when parsing the file - or do you insist on creating a (batch) file doing the work later?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36511846
Then for the Log drives I need to parce the mountpoints a little farther as I can't have the db11 in the name rather it will need to be L:\db1_Log11 for disk 003

Is it really  L:\db1_Log11, or rather  L:\d1_Log11 ?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 8

Author Comment

by:brittonv
ID: 36511858
It can be a series off files.
I
One to create roots
Then the second could create the mountpoints in powershell while it creates the second text file for diskpart.

Whatever you recommend
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36511955
The sequence for creating L: is the same as for D:, I assume?
And besides an answer to http:#a36511846 I need the diskpart procedure for RESTORE.
0
 
LVL 8

Author Comment

by:brittonv
ID: 36512143
Yes the sequence is the same.  And yes it l:D1_###. Good catch

I don't follow the restore procedure you are looking for.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36512154
How do we create the R: partition?
0
 
LVL 8

Author Comment

by:brittonv
ID: 36512203
Same procedure as the others. Once you provide the script for creating the others I'll be able to modify it to create the R
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36512307
This script now generates a batch file (makepart.cmd) containing all necessary commands. Since many parts are very similar to each other, I have used functions (Roots, DataOrLog) doing the "real" stuff.
$disks = Import-Csv "c:\temp\ee\diskpart.txt" -header DiskNo, Lun
$disks | ? {$_.Lun -like "*root_data"} | % { Roots D: $_.DiskNo $_.Lun | Out-File "makepart.cmd" }
$disks | ? {$_.Lun -like "*root_log" } | % { Roots L: $_.DiskNo $_.Lun | Out-File -Append "makepart.cmd" }
$disks | ? {$_.Lun -like "*_restore" } | % { Roots R: $_.DiskNo $_.Lun | Out-File -Append "makepart.cmd" }

$disks | ? {$_.Lun -like "*_*_*_data"} | % `
{
	$dir = "D:\" + ($_.Lun.Split("_")[0..1] -join "_")
	DataOrLog $dir $_.DiskNo  $_.Lun | Out-File -Append "makepart.cmd"
}

$disks | ? {$_.Lun -like "*_*_*_log" } | % `
{
	$dir = "L:\" + ($_.Lun.Split("_")[0..1] -join "_").replace("DB","Log")
	DataOrLog $dir $_.DiskNo $_.Lun | Out-File -Append "makepart.cmd"
}

function Roots ($Drive, $DiskNo, $Lun)
{
	"(echo select $DiskNo"
	" echo online disk"
	" echo attribute disk clear readonly"
	" echo create partition primary"
	" echo format fs=ntfs Label=" + $Lun
	" echo Assign Letter=$Drive"
	") | diskpart "
}

function DataOrLog ($dir, $DiskNo, $Lun)
{
	"md $dir"
	"(echo select $DiskNo"
	" echo online disk"
	" echo attribute disk clear readonly"
	" echo create partition primary"
	" echo format fs=ntfs Label=$Lun"
	" echo assign mount=$dir"
	") | diskpart"
}

Open in new window

0
 
LVL 8

Author Comment

by:brittonv
ID: 36512429
Wow, looks great!  

when I run it however I get the following error:
The term 'DataOrLog' is not recognized as the name of a cmdlet, function, script file, or operable program.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36512466
That is strange, because I used the code exactly as posted, and it worked for me ... Try putting both function definitions at the top of the script.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now