VMware Cloud Community
AnthBro
Enthusiast
Enthusiast
Jump to solution

Scsi address and null field issues when adding drives

I have got a spreasheet and it requires up to 6 drives to be added to a list of VMs.

vmname, hdd1, hdd2, hdd3, hdd4, hdd5, hdd6. I then want to run another command if hdd2 exists lets say its to return "disk two exists"

hdd1 always needs to be mapped to scsi 0:3,

hdd2 always needs to be mapped to scsi 0:4 etc.

but sometime there is a hdd1 and a hdd3 and hdd6 but no hdd2 or hdd 4.

I have two problems

I don't know how to ensure the drive gets assigned the write scsi address.

I don't know how to do or not do things if fields are null or not.

Please help.

Any views or opinions presented in this post are solely those of the author and do not necessarily represent those of the company he works for.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can specify a SCSI nummer if you add a hard disk using the vSphere API:

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "add"
$spec.deviceChange[0].fileOperation = "create"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device.key = -100
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$spec.deviceChange[0].device.backing.fileName = ""
$spec.deviceChange[0].device.backing.diskMode = "persistent"
$spec.deviceChange[0].device.backing.split = $false
$spec.deviceChange[0].device.backing.writeThrough = $false
$spec.deviceChange[0].device.backing.thinProvisioned = $false
$spec.deviceChange[0].device.backing.eagerlyScrub = $false
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $false
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.controllerKey = 1000
$spec.deviceChange[0].device.unitNumber = 5
$spec.deviceChange[0].device.capacityInKB = 1048576

$vm = Get-VM -Name MyVM
$vm.ExtensionData.ReconfigVM_Task($spec)

The above code adds a hard disk with SCSI number 5 to a vm called MyVM. The line:

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

defines the SCSI number to 5. The 0 in the SCSI number 0:5 is defined by the SCSI controller. So if you only have one SCSI controller then the first number will always be 0.

I am not sure what you mean with "I don't know how to do or not do things if fields are null or not.".

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
5 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can specify a SCSI nummer if you add a hard disk using the vSphere API:

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "add"
$spec.deviceChange[0].fileOperation = "create"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device.key = -100
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$spec.deviceChange[0].device.backing.fileName = ""
$spec.deviceChange[0].device.backing.diskMode = "persistent"
$spec.deviceChange[0].device.backing.split = $false
$spec.deviceChange[0].device.backing.writeThrough = $false
$spec.deviceChange[0].device.backing.thinProvisioned = $false
$spec.deviceChange[0].device.backing.eagerlyScrub = $false
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $false
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.controllerKey = 1000
$spec.deviceChange[0].device.unitNumber = 5
$spec.deviceChange[0].device.capacityInKB = 1048576

$vm = Get-VM -Name MyVM
$vm.ExtensionData.ReconfigVM_Task($spec)

The above code adds a hard disk with SCSI number 5 to a vm called MyVM. The line:

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

defines the SCSI number to 5. The 0 in the SCSI number 0:5 is defined by the SCSI controller. So if you only have one SCSI controller then the first number will always be 0.

I am not sure what you mean with "I don't know how to do or not do things if fields are null or not.".

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
AnthBro
Enthusiast
Enthusiast
Jump to solution

Hi Rob,

Thank you so much for your script and I can see how to add it using the SCSI numbers now i.e. change the controller and SCSI vaules.

The last part I'm not sure on is, how do I get a script to do something if the HDD2 field is null.

Something like

If $hdd2 = null then do this

else do this

but in a powershell/powercli terminology.

I understand I need to buy a book and learn the basics, I am in the process of doing this now.

But if I can understand the above, I should be able to get out of trouble until I can read it.

Thanks

Anthony


Any views or opinions presented in this post are solely those of the author and do not necessarily represent those of the company he works for.
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Anthony,

your functional code will be translated into the following PowerCLI example:

$vm = Get-VM -Name MyVM
if (-not (Get-HardDisk -VM $vm |
  Where-Object {$_.Name -eq "Hard Disk 2"}))
{
  $true
}
else
{
  $false
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
AnthBro
Enthusiast
Enthusiast
Jump to solution

Hi, thats outstanding, thanks so much..

May ask one further thing, sorry to push the friendship, the original script to add the disk, can you is it possible to have an RDM version?

I will have the canonical name of the lun from the storage team and need to create the disks on scsi 1:0.

Please.... :smileygrin:

Any views or opinions presented in this post are solely those of the author and do not necessarily represent those of the company he works for.
0 Kudos
MR-Z
VMware Employee
VMware Employee
Jump to solution

The "onyx" tool probably can answer most of your questions. You can download it from labs.vmware.com (under flings).

0 Kudos