From our VI Toolkit snippet-vault.
The following 2 functions do probably most of what you want.
Function add-HD uses an existing .vmdk file to create a hard disk on a VM.
The calling syntax: add-HD <VM-name> <datastore-name> <path-to-vmdk> <controller-label>
An example: add-HD "PC4" "vmfs2" "PC4/PC4.vmdk" "SCSI Controller 0"
Function remove-HD removes a hard disk from a VM and optionally deletes the .vmdk file.
The calling syntax: remove-HD <VM-name> <hard-disk-label> <delete-flag>
An example: remove-HD "PC4" "Hard Disk 3" $TRUE
The code:
Get-VIServer -Server <VC-server>
[http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("vmware.vim")
function remove-HD {
param($VMname, $HDname, $Delflag)
$vm = Get-View (Get-VM $VMname).ID
foreach($dev in $vm.Config.Hardware.Device){
if ($dev.DeviceInfo.Label -eq $HDname){
$key = $dev.Key
$name = $dev.Backing.FileName
}
}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = @()
$spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDevice
$spec.deviceChange[0].device.key = $key
$spec.deviceChange[0].operation = "remove"
$vm.ReconfigVM_Task($spec)
if ($Delflag){
$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$fileMgr = Get-View $serviceInstance.Content.fileManager
$datacenter = (Get-View (Get-VM $VMname | Get-Datacenter).ID).get_MoRef()
$fileMgr.DeleteDatastoreFile_Task($name, $datacenter)
}
}
function add-HD {
param($VMname, $DSname, $Filename, $SCSIcntrl)
$vm = Get-View (Get-VM $VMname).ID
$ds = Get-View (Get-Datastore -Name $DSname).ID
foreach($dev in $vm.config.hardware.device){
if ($dev.deviceInfo.label -eq $SCSIcntrl){
$CntrlKey = $dev.key
}
}
$Unitnumber = 0
$DevKey = 0
foreach($dev in $vm.config.hardware.device){
if ($dev.controllerKey -eq $CntrlKey){
if ($dev.Unitnumber -gt $Unitnumber){$Unitnumber = $dev.Unitnumber}
if ($dev.key -gt $DevKey) {$DevKey = $dev.key}
}
}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = @()
$spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$spec.deviceChange[0].device.backing.datastore = $ds.MoRef
$spec.deviceChange[0].device.backing.fileName = "[" + $DSname + "] " + $Filename
$spec.deviceChange[0].device.backing.diskMode = "independent_persistent"
$spec.deviceChange[0].device.key = $DevKey + 1
$spec.deviceChange[0].device.unitnumber = $Unitnumber + 1
$spec.deviceChange[0].device.controllerKey = $CntrlKey
$spec.deviceChange[0].operation = "add"
$vm.ReconfigVM_Task($spec)
}
Since the forum SW apparently has a mind of it's own I have attached the code.
Note that the functions can definitely be improved.
Currently the fail-safe mechanisms are quit limited.
If there is a need to explain parts of the code please let me know.
And of course, use in a production environment at your own risk ! :smileywink: