VMware Cloud Community
tonygent
Enthusiast
Enthusiast
Jump to solution

Adding VMDK's to VM's in new vSphere PowerCLI

Hi All,

This message goes hand in hand with may last message and involve the uncomfortable transferal to the new vSphere and PowerCLI from the VItoolkit on VMware 3.5.

One of my scripts uses a couple of functions to add existing VMDK files to an existing VM. The big problem is - this code is for some reason not valid in the new Power CLI.

As you can see - the functions have simple command line with VM, VMDK file, and SCSI Controller number. Can anyone hazard a guess while this process is no longer valid for PowerCLI??

Thanks in anticipation 🐵

  1. Functions

###Examples

      1. add-HD "PC4" "vmfs2" "PC4/PC4.vmdk" "SCSI Controller 0"

      2. remove-HD "VM-name" "Hard Disk 3" $TRUE

function add-HD {

param($VMname, $DSname, $Filename, $SCSIcntrl)

#write-host " Adding Disk $Filename from Datastore $DSname to VM $VMname"

$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}

}

}

logentry $sLogFile "DeviceKey is $DEVKEY, UnitNUm is $UnitNUmber, ContrlKey is $CntrlKey"

$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 = " " + $Filename

$spec.deviceChange[0].device.backing.diskMode = "independent_persistent"

$spec.deviceChange[0].device.key = $DevKey + 1

    1. The UnitNUmber SCSIID 7 is reserved for the Controller - so ignore it and skip to 8.

if ($Unitnumber -eq 6) {$Unitnumber = $Unitnumber +1}

$spec.deviceChange[0].device.unitnumber = $Unitnumber + 1

$spec.deviceChange[0].device.controllerKey = $CntrlKey

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

$vm.ReconfigVM_Task($spec)

}

function remove-HD {

param($VMname, $HDname, $Delflag)

#write-host " Removing Disk $HDname for VM : $VMname"

$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)

}

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Below the new versions of my Add-HD and Remove-HD functions.

The Add-HD functions had an error in this line

	$spec.deviceChange[0].device.backing.fileName =" " + $Filename

The Remove-HD function can be optimised in vSPhere.

With the help of the LayoutEx.Disk property you can now find all the files that are behind a virtual disk.

The new versions are attached since there are some square brackets in the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
12 Replies
harkamal
Expert
Expert
Jump to solution

Hi tonygent, can you post snapshot of actual script execution, to get to know which cmdlets/APIs are failing or any exceptions. We can then work this out.

By the way, there's easy way of getting serviceInstance, you dont have to create a a managedobject, set its name/value etc. try..

$si = Get-View ServiceInstance

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Below the new versions of my Add-HD and Remove-HD functions.

The Add-HD functions had an error in this line

	$spec.deviceChange[0].device.backing.fileName =" " + $Filename

The Remove-HD function can be optimised in vSPhere.

With the help of the LayoutEx.Disk property you can now find all the files that are behind a virtual disk.

The new versions are attached since there are some square brackets in the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tonygent
Enthusiast
Enthusiast
Jump to solution

Hi LucD

From what I can see the add script looks exactly the same, the remove has changed though. Wonder if something has changed when I printed it to the screen, but in the end, looks like it's a "Working perfectly error" anyway. I've retried my test with the original code (after I'd posted this message) and it worked fine?? Think the Powershell must have got it's code twisted up ??

However - I'm very grateful for the updated scripts and I'll put them to good use.

If you could take a look at my other post (posted the same day as this one) on mounting snapped LUN's on vSphere - I'd be very grateful.

Thanks

TG

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Hi Tony, the problem is most probably caused by the forum SW which has problems when code contains square brackets.

I'll have a look at your other post.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tonygent
Enthusiast
Enthusiast
Jump to solution

Thanks LucD

Hope you can help Smiley Happy

Tony Gent

VCP, ACE, CNE, CCA

M: 0776 4485005

T: 01932 724461

F: 01932 724010

W: http://www.snsltd.co.uk

Helpdesk

T: 08456 022118

E: helpdesk@snsltd.helpserve.com

W: http://snsltd.helpserve.com

Simply Networking Solutions

Virtualisation, Backup and Disaster Recovery specialists

Upcoming events:-

Understand the next-generation virtualisation technologies from VMware and EMC

Free half day technical update

Find out more and register at http://www.snsltd.co.uk/virtualisation.html

Are you looking to cost-effectively improve your VMware Infrastructure 3 and vSphere 4 knowledge?

Why not take a look at our VMware vSphere 4 Fast-track training

P  please consider the environment before printing this email!

The information contained in this email and its attachments may be confidential. If you are not the intended recipient, any use, disclosure or copying of any part of this email is unauthorised. If you have received this email in error, please notify the originator immediately.

0 Kudos
regis
Contributor
Contributor
Jump to solution

Hello
Thank you for you "add / remove hd" script (Luc) , but you know if there is a way for change vmdk path on a particular disk with "ReconfigVM_Task", without use remove/add HD functions
spec.deviceChange.operation = "change" instead of "add" or "remove" ? exist ?
$spec.deviceChange[0].device.backing.fileName ="new vmdk full filename"
...
...
ReconfigVM_Task ...
I need this one after LUN replica (the UUID changed next the vmdk path is not correct after the VMRegister).
thank you
Régis

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Had a quick look at the ReconfigVM_Task method.

For the operation property there is "add","edit" and "remove".

For the fileOperation property there is "create","destroy" and "replace".

It looks as if the operation=ëdit" and fileoperation="replace" should be able to do the trick.

The backing.filename should then point to the new .vmdk file (in the "\[datastore\] Guest1/disk1Snap.vmdk" notation.

Haven't tried it out yet though.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
EdZ
Contributor
Contributor
Jump to solution

LucD - Those Add-HD and Remove-HD functions are really helpful. I have a couple of questions on the script - should this be opened as a separate thread? I can almost follow all of the commands in the scripts (I'll get there one day Smiley Happy ). From what I can tell, using the VI objects to create a new disk only requires a few parameters. I get the first section of code below, which locates the object info on the SCSI Controller specified in the input (e.g. "SCSI Controller 1") and gets the key number,

foreach($dev in $vm.config.hardware.device){

if ($dev.deviceInfo.label -eq $SCSIcntrl){

$CntrlKey = $dev.key

}

}

but I'm not 100% clear on what the the following section of code does

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}

}

And in the code following, it looks like a new key for a SCSI Controller is being referenced, but I'm trying to figure out if it is the same controller if specified in the input to the function, since there is a line

$spec.deviceChange[0].device.key = $DevKey + 1

which appears to be operation on the SCSI controller with the next key #? I think I follow the next part

$spec.deviceChange[0].device.unitnumber = $Unitnumber + 1

which assigns the next available Unit Number to the disk.

The reason I'm trying to understand this is not just academic - I'm hoping to adapt the function to create an RDM based on some input including the $VM.Config.Hardware.Device.Backing.DeviceName (for example "vmhba1:4:46"), and assign it to a specific $VM.Config.Hardware.UnitNumber (e.g. unit number 6). This becomes important when recreating and RDM's when the Canonical name may have changed, but the underlying disk device has not, and it needs to be presented as the same SCSI device to the OS. Can you give a hint on how this could be done using the current function?

Thanks,

Ed Z

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the first loop the script gets the device key of the SCSI controller into variable $CntrlKey.

In the 2nd loop the script will find the highest (numeric) unit number and device key of the devices that are already connected to the SCSI controller.

When setting up the properties for the new HD the following needs to be done:

*) the device key is set to the highest device key (on that SCSI controller) plus 1 (which makes sure the device key is free/unused)

*) same for the unit number with the exception that unit number 7 is reserved for the SCSI controller itself. Hence the extra test.

*) the controller key is the device key of the SCSI controller (found in the first loop)

I hope this clarified it a bit more.

Attached the vSphere version of the script with some extra comments.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
regis
Contributor
Contributor
Jump to solution

Hello Luc

arghhh!

have sample ? Smiley Happy

thank you

Régis

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are 2 commented out sample calls at the end of the attached script.

Or is that now what you're asking ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
regis
Contributor
Contributor
Jump to solution

oup's .. so sorry

for my issue with "ReconfigVM_Task " or how to change vmdk path with ReconfigVM_task

0 Kudos