top of page

TechBobbles

tech logo with triangle shape.jpg
  • Siddartha Kumar Das

Adding Exiting Disk With specified SCSI Controller ID using PowerCli



I came up with a new script for adding a virtual disk to the VM with the specified ID. So let's get into the code without delay.



##########################Input Block########################

$strVMToUpdate = "


XXXXXX"

$disk_path= "[LtXXXXXXXSSXXP01] vwXXXX_1/vwltXXXXX.vmdk"

$scsi_controller = "SCSI controller 3"

$scsi_number = 8

##################Connecting to vCenter########################

Connect-VIServer XXXXXXX -User XXXXXXX -Password XXXXX

#Get the VM

$vmToUpdate = Get-VM $strVMToUpdate

#get the SCSI adapter already connected and get the total count of it , in one VM maximum 4 scsi adapter can come

$Scsi = $vmToUpdate | Get-ScsiController

[int]$count = 4 - ($scsi.Count)

#Creating a for loop so which will create new disk of 5GB by using new SCSI controller(here the motive is to create scsi controller)

for ($i =0; $i -lt $count ; $i++)

{

#Creting new disk with new controller(please specify the controller details correctly)

New-ScsiController -HardDisk ($h = New-HardDisk -VM $vmToUpdate -CapacityGB 5 ) -BusSharingMode virtual -type VirtualLsiLogicSAS

#Get the correct disk details in a array which are created in the process

$counter += @($h)

}

#take break(mostly not required)

Start-Sleep -Seconds 60


#Now the time to add/connect the exiting disk the right controller as per our input

$hdskToChange = New-HardDisk -VM $vmToUpdate -DiskPath $disk_path -Controller $scsi_controller

##The below part i copied from (https://communities.vmware.com/thread/421110 ) wich will help you select the right unit number in the scsi controller. You can get the same kind of code from Onyx also.

## create a new VirtualMachineConfigSpec, with which to make the change to the VM's disk

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

## create a new VirtualDeviceConfigSpec

$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].operation = "edit"

## populate the "device" property with the existing info from the hard disk to change

$spec.deviceChange[0].device = $hdskToChange.ExtensionData

## then, change the second part of the SCSI ID (the UnitNumber)

$spec.deviceChange[0].device.unitNumber = $scsi_number

## reconfig the VM with the updated ConfigSpec (VM must be powered off)

$vmToUpdate.ExtensionData.ReconfigVM_Task($spec)

##Now that all the configurations is completed, Now we can remove the disk which we made just to create a scsi controller.

# The counter array holds all the disk details which created and i just did a froeach loop

foreach($disk in $counter)

{

Write-Host "Removeing the disk Disk "$disk.name""

#Remove the unnecessary disk permanently

$vmToUpdate| Get-HardDisk | where {$_.filename -eq ($disk.Filename)} |Remove-HardDisk -DeletePermanently -Confirm:$false -ErrorAction:SilentlyContinue

}

#Remove the variable


Remove-Variable -Name counter



If anyone has multiple disks to add, it is better to practice to keep the inputs(Vceneter,user,pass,disk location,scsi controller number, unit number) in the .CSV and import it and do a for each loop.

Thanks for coming to my site.

Thanks again,

Siddartha Kumar Das

Contact Number - +919148014455


Thanks for visiting 

Bangalore,

Siddartha1192@gmail.com

+919148014455

  • Linkedin
  • Wix Facebook page
  • Wix Twitter page

Subscribe to get exclusive updates

bottom of page