I'm working on creating a Powershell script that will ultimately become a post install script on newly provisioned virtual machines, for both VMware and Hyper-V. My first task is to deal with any disks that were added per the original request for a new server. For now, let's just assume that they will all be Server 2012. When someone requests a server it could have anywhere from 1 to 5 virtual hard drives. I've been able to successfully create and run a script that *mostly* does what I want it to do. Which are the following tasks:
1. Online the disks
2. Initialize the disks
3. Partition the disks
4. Format the volumes
I've had some limited success doing this, but the problem is that the volumes don't always get put in the right order based on the disk number, which is how I need them to be. My solution is to build an array and perform the necessary tasks one at a time. My thought is that I need to store the disk number in order to do this but I'm not sure how to store the disk number in a variable. Any suggestions would be welcomed. I've pasted what I have so far and you'll notice that it's pretty unorganized, but should tell the story. :)
#Change CDROM drive letter from current drive letter to Z:\(gwmi Win32_cdromdrive).drive | %{$a = mountvol $_ /l;mountvol $_ /d;$a = $a.Trim();mountvol z: $a}
Get-Disk | Where-Object IsOffline -Eq $true | Set-Disk -IsOffline $false
$RAW = Get-Disk | Where PartitionStyle -Eq "RAW"
$RAW | ForEach-Object {$_; Start-Sleep 5; }
#Initialize-Disk -Number $ND -PartitionStyle MBR -PassThru;
#New-Partition -AssignDriveLetter -UseMaximumSize;
#Format-Volume -FileSystem NTFS -Confirm:$false }
#$DiskNum = 0; $ND = $DiskNum + 1; Initialize-Disk -Number $ND -PartitionStyle MBR -PassThru
get-disk | Where-Object -FilterScript {$_.Number -