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.
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.".
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.".
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
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 }
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:
The "onyx" tool probably can answer most of your questions. You can download it from labs.vmware.com (under flings).