Reputation: 323
I drummed up the following code to initialize new disks that are added to a Windows VM:
$newdisk = get-disk | where partitionstyle -eq 'raw'
foreach ($d in $newdisk){
$disknum = $d.Number
$dl = get-Disk $d.Number | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize
Format-Volume -driveletter $dl.Driveletter -FileSystem NTFS -NewFileSystemLabel "Disk $disknum" -Confirm:$false
}
Instead of using the disk number for the file system label, I want to use some predefined volume names, like "OS", "Data", "System", etc....
I tried $name = "OS", "Data", "System" and put that variable in place of $disknum, but that just added the whole string as the volume name as it looped through.
Am I missing something? Should it be a variable inside of the loop? How can I get it to read each name from the variable for each run of the disk initilization inside the loop ?
I am newish to powershell and seeing examples helps me quite a bit.
Upvotes: 1
Views: 8781
Reputation: 1
$disknum = @(get-disk | Where-Object partitionstyle -eq 'raw'| Where-Object {$_.Size /1GB -match '20'})
$dl = get-Disk $disknum.Number |
Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize
Format-Volume -driveletter F -FileSystem NTFS -NewFileSystemLabel 'DB Data' -Confirm:$false
Upvotes: 0
Reputation: 2904
Since i do not know how many drives you are working with i included some dummy entries in the labels
array.
this uses a for loop instead of a foreach thus preventing the need for a nested loop structure.
$newdisk = @(get-disk | Where-Object partitionstyle -eq 'raw')
$Labels = @('OS','Data','System','OS','Data','System')
for($i = 0; $i -lt $newdisk.Count ; $i++)
{
$disknum = $newdisk[$i].Number
$dl = get-Disk $disknum |
Initialize-Disk -PartitionStyle GPT -PassThru |
New-Partition -AssignDriveLetter -UseMaximumSize
Format-Volume -driveletter $dl.Driveletter -FileSystem NTFS -NewFileSystemLabel $Labels[$i] -Confirm:$false
}
Upvotes: 2