Skip to main content

vSphere PowerCLI: Moved your ISO datastore ? Reconnecting your CD-ROM drives

We recently moved our ISO store from a legacy NFS server to our main NFS filer.

The first task was copying the actual files, which can be done via any machine that has both datastores mounted (with read-write access to the destination store).

The more significant job is reconfiguring the VMs to use the copies of the ISOs in the new datastore.

Here’s the PowerCLI script I used:

  1. #
  2. # Disconnect all CD/DVD ISOs on a particular datastore and reconnect them to the new datastore
  3. #
  4. $myvcenter = "myvcenter.host.net"
  5. $mydatacenter = "BigDC1"
  6. $originalISOstore = "MyFirstISOstore"
  7. $finalISOstore = "MuchBetterISOstore"
  8.  
  9. Connect-VIServer $myvcenter
  10.  
  11. (Get-VM -Location: (Get-Datacenter $mydatacenter)) | ForEach ( $_ ) 
  12. 	{ 
  13. 	$myDrive = Get-CDDrive $_
  14. 	if ($myDrive.IsoPath -match "\["+$originalISOstore+"\]") 
  15. 		{
  16. 		Set-CDDrive -CD $myDrive -IsoPath ($myDrive.IsoPath -replace $originalISOstore, $finalISOstore) -Confirm:$false  
  17. 		} 
  18. 	}
  19. Disconnect-VIServer $myvcenter -confirm:$false

It’s fairly self-explanatory.

The biggest caveat with this is that it assumes that the source and destination stores have the same structure, but it wouldn’t be difficult to amend it to change the destination path slightly.

Add multiple datastores to multiple vSphere hosts

In large vSphere environments it can be very tedious to add multiple NFS datastores to lots of hosts.
PowerCLI comes to the rescue as usual.

I needed to add some datastores to all the clustered hosts in a single datacenter, but to skip our non-clustered standalone hosts which are used for backups.

A bit of PowerCLI which should be fairly self-explanatory:

  1. #
  2. #  Add Datastores to all hosts in all clusters in a specified datacenter
  3. #  If a host isn't in a cluster it won't get the datastore
  4. #  Easy enough to change to do all hosts in a datacenter, all in vCenter etc
  5. #
  6. #  Change the value below to point it to your own vCenter
  7. $myvCenter = "vcenter.example.com"
  8.  
  9. # Array of arrays below holds NFS-hostname, NFS-path and Datastore name, should be easy to add to
  10. $nfsArray = @(
  11. 			  @("nfsserver1","/vol/nfspath1","VMstore1"),
  12. 			  @("nfsserver2","/vol/nfspath2","VMstore2"),
  13. 			  @("nfsserver3","/vol/nfspath3","VMstore3")
  14. 			 )
  15.  
  16. $datacenter = Read-Host -Prompt "Datacenter name"
  17.  
  18. Write-Host "Datacenter is $datacenter"
  19.  
  20. connect-viserver -server $myvCenter
  21.  
  22. #
  23. # Get all hosts in all clusters in the named datacenter
  24. $ObjAllHosts = get-datacenter -name $datacenter | Get-Cluster  |  Get-VMHost 
  25.  
  26. ForEach($objHost in $ObjAllHosts){
  27. 	ForEach($nfsItem in $nfsArray) {
  28.  
  29.       Write-Host "Adding datastore" $nfsItem[2] "with path" $nfsItem[0]":"$nfsItem[1] "to" $objHost
  30.       New-Datastore -Nfs -NfsHost $nfsItem[0] -Path $nfsItem[1] -Name $nfsItem[2] -VMHost (Get-VMHost $objHost)
  31.     }
  32. }
  33. disconnect-viserver -server $myvCenter -Confirm:$false

Passing info from PowerCLI into your VM using guestinfo variables

For a project at work we’ve been trying to pass information into a VM without connecting the VM to the network.
This is in order to set up some config within both Windows and Linux VMs. We decided to explore the use of GuestInfo variables which are held in memory in VMware Tools within the Guest VM, but which can be set from the host.

From the ESX Service Console of the host you can use
vmware-cmd <cfgfile> setguestinfo <variable> <value>
to set a value and vmware-cmd <cfgfile> getguestinfo <variable> to read the value. Note that you don’t need to use the “guestinfo.” prefix when using these commands.

Within the VM guest OS the values can be set/read using:
(Windows)
vmtoolsd.exe --cmd "info-set guestinfo.<variable> <value>"
vmtoolsd.exe --cmd "info-get guestinfo.<variable>"
(vmtoolsd.exe is usually in C:\Program Files\VMware\VMware Tools)

(Linux)
vmware-guestd --cmd "info-set guestinfo.<variable> <value>"
vmware-guestd --cmd "info-get guestinfo.<variable>"
(vmware-guestd is usually in /usr/sbin)

It wasn’t immediately obvious where the guestinfo variables live with regard to the PowerCLI vmConfig properties. Googling didn’t reveal much useful info, so I though I’d try the wonders of new technology and use Twitter. I’ve been following Carter Shanklin of VMware on Twitter since I attended a London UK VMware User Group meeting a couple of months ago, and as he’s the Product Manager for PowerCLI and the SDK, I thought I’d ask.

He quickly pointed me at the VMware SDK documentation for the ConfigInfo object and the extraConfig object in particular.

A bit of further reading led me to some experiments let me to try the following code:
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$extra = New-Object VMware.Vim.optionvalue
$extra.Key="guestinfo.test"
$extra.Value="TestFromPCLI"
$vmConfigSpec.extraconfig += $extra
$vm = Get-View -ViewType VirtualMachine | where { $_.name -eq "MyVMName" }
$vm.ReconfigVM($vmConfigSpec)

That will set the guestinfo.test property to “TestFromPCLI”. Once that’s been set it can be read by the VM.

The guestinfo property can have multiple Key/Value pairs so you can pass quite a few variables through to a VM. These can only be set when a VM is powered up and running VMware Tools as the value is stored in the VMs memory, and as far as I can tell, the contents are lost when the VM reboots.

However, there is another extraConfig object which can also be set which is the machine.id. Again this can be read from within the VM (replace guestinfo. with machine.id in the above code snippets), but this one gets written to the VMs VMX config file and will thus survive reboots.

You could squish several bits of info into that one object/variable, for example a unique identifier and the VM name so that the VM can self-configure.