Paginas

10 November 2017

Create Multiple Mount Points in Multiple Disks in one Datastore VMware



I was in a costumer where I had to create many disks to present to several virtual machines in VMware. The Datastores were few, but I had to create 60 disks in each VM (maximum supported with four SCSI controllers). To increase complexity, all of these Volumes in Windows meant to be Mount Points. To optimize this process I had to create some Scripts to help me.

I splited in different points:
  • Script to add multiple disks from one datastore to a VM;
  • Script to change all the Disks paths to Round Robin (The Storage was a HPE 3PAR);
  • Script to Create all the Windows Mount Points;
  • Script to view the Mount Points created.
Let's see how to do it!

Add multiple disks from one datastore to a Virtual Machine
### All you need to insert is the VM name, number of Disks, Datastore Location and the Disk Size

$vmname = read-host "VM Name to add disks to"
$num_disks = read-host "number of disks to add"
$ds = read-host "Datastore to place the VMDK"
$format = read-host "Disk Format (thin, thick, EagerZeroedThick)"
$size = read-host "Disk Size (GB)"

$vm = get-vm $vmname
$datastore = get-datastore -name $ds
$x=0
while ($x -lt $num_disks){
write-host "Adding $size VMDK to $vm on datastore $datastore"
New-HardDisk -vm $vm -CapacityGB $size -Datastore $datastore -StorageFormat $format
$x++
}


Change all the Paths to Round Robin
### First Install VMware PowerCLI that corresponds to the vSphere version that you are using

Connect-VIServer NAME_OF_VCENTER
Get-Vmhost | Get-Scsilun
Get-VMHost | Get-ScsiLun -LunType disk | Where {$_.MultipathPolicy -notlike "RoundRobin"}
Get-VMHost | Get-ScsiLun -LunType disk | Where {$_.MultipathPolicy -notlike "RoundRobin"} | Set-Scsilun -MultiPathPolicy RoundRobin


Create Windows Mount Points
### All you need to insert is the number of the disk (you can view that in Disk Management), the Volume Name that you want and the Mount Point path location

Function Create-MountPoint
{
    Param($numberDisk,$VolumeName,$Path)
 New-Item -Path $Path -ItemType Directory
    $disk = Get-Disk $numberDisk
 $disk | Initialize-Disk -PartitionStyle GPT -PassThru
 $disk | New-Partition -UseMaximumSize -AssignDriveLetter:$False
 $partition = Get-Partition -DiskNumber $disk.Number
 $partition | Format-Volume -AllocationUnitSize 64KB -FileSystem NTFS -NewFileSystemLabel $VolumeName -Confirm:$false
 $partition | Add-PartitionAccessPath -AssignDriveLetter:$False
 $partition | Add-PartitionAccessPath -AccessPath $Path
 $partition | Set-Partition -NoDefaultDriveLetter:$true
}

Use the import-module <Name_of_the script>
Then tou can use the Create-MountPoint function like for example (this will create a Mount point in Disk 5 to c:\teste\01 with Vlume name TEST):
Create-MountPoint 5 TEST c:\teste\01

View Windows Mount Points
### All you need to do is import this module

Function GetVolumeMountPoints
{
    Foreach($Computer in $ComputerName)
    {
        $params = @{Class = "Win32_Volume"
                    ComputerName = "$Computer"
                    Filter = "DriveType='3'"}
        $volumes = Get-WmiObject @params
        $volumes | Where DriveLetter -EQ $null | Select @{Name="ComputerName";Expression={$_.PSComputerName}},`
        @{Name="MountPoints";Expression={$_.Caption}},`
        @{Name="Capacity";Expression={"{0:N2}" -f ($_.Capacity/1GB) + " GB"}},`
        @{Name="UsedSpace";Expression={"{0:N2}" -f (($_.Capacity/1GB) - ($_.FreeSpace/1GB)) + " GB"}},`
        @{Name="FreeSpace";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) + " GB"}},`
        @{Name="Available(%)";Expression={"{0:P2}" -f (($_.FreeSpace/1GB)/($_.Capacity/1GB))}}|`
        Where MountPoints -NotLike "\\?\Volume*" | Format-Table -AutoSize
    }
}

Use the import-module <Name_of_the script>
Then tou can use the GetVolumeMountPoints function without arguments to view all the information.
GetVolumeMountPoints

Something like this will appear:

Computer MountPoints   Capacity  UsedSpace  Free  Available(%)
------------  --------------------   -------------   ---------  -------------  ---------
SERVER1  M:\MOUNT001\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT002\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT003\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT004\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT005\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT006\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT007\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT008\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT009\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT010\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT011\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT012\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT013\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT014\  2047,87GB  0,24GB  2047,64GB 99,99%
SERVER1  M:\MOUNT015\  2047,87GB  0,24GB  2047,64GB 99,99%



I hope I have helped you and saved a lot of time and work. :)
See you next time!

No comments:

Post a Comment