Richard Parmiter

Virtualisation blog and Knowledge Base

  • You are here: 
  • Home
  • VMWare

Add VMs to vCenter from an unknown datastore

Posted on October 17th, 2012

Sometimes it may be required to add VMs to vCenter from a recovered or new datastore. This can be performed manually by adding a new VM and browsing the datastore to attach the disk from the .vmdk file or adding the .vmx files.

However, for datastores that contain many virtual machines, this is laborious and time consuming and it is beneficial to script this where possible.

A script to do this is as follows:

$Cluster = “vCenter Cluster Name”
$Datastores = “vSphere Datastore Name”
$VMFolder = “VM Folder Name”
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

foreach($Datastore in Get-Datastore $Datastores) {
# Set up Search for .VMX Files in Datastore
$ds = Get-Datastore -Name $Datastore | %{Get-View $_.Id}
$SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$SearchSpec.matchpattern = “*.vmx”
$dsBrowser = Get-View $ds.browser
$DatastorePath = “[” + $ds.Summary.Name + “]”

# Find all .VMX file paths in Datastore, filtering out ones with .snapshot (Useful for NetApp NFS)
$SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch “.snapshot”} | %{$_.FolderPath + ($_.File | select Path).Path}

#Register all .vmx Files as VMs on the datastore
foreach($VMXFile in $SearchResult) {
New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $Folder -RunAsync
}
}

It is also a useful script if replicating virtual machines to backup storage for DR purposes. If you ever need to use the backup storage, simply attach it to the vSphere cluster and run the script to quickly add all the VM’s that reside on it.

Ref: I found this script on the following blog: Wood IT Work

Tags: , , , ,
Filed under Scripts/Reg, VMWare | No Comments »

Determine vCenter display name from within the VM

Posted on October 17th, 2012

There may be situations where you want to determine the vCenter diaply name for a VM from within the OS of the VM.

This could be useful if cloning multiple virtual desktops from a syspreped template to enable the option to set the machine name to be the same as the vCenter display name. It could also be useful in many other scenarios.

However, by default there is no way to do this using the standard VM tools that are instaled into the virtual machine.

It is possible though to set a custom attribute on the VM object in vCenter and then query this from within the the OS of the virtual machine.

The following script can be run using the vSphere PowerCLI to set a custom attribute to be the same as the vCenter display name:

$vServer= “vCenter.server.fqdn”
$vmName = “VM display name”

If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}

Connect-VIServer $vServer | out-null

$vmSet = GET-VM $vmName | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$gInfo = New-Object VMware.Vim.optionvalue
$ginfo.Key=”guestinfo.hostname”
$gInfo.Value=$vmSet.Name
$vmConfigSpec.extraconfig += $gInfo
$vmSet.ReconfigVM($vmConfigSpec)

Disconnect-VIServer $vServer -Confirm:$false | out-null

Once this is set, it is possible to query it within the VM, using the VM tools and the following command:

vmtoolsd.exe –cmd “info-get guestinfo.hostname”

This can of course, be added to scripts to set this attribute against multiple machines.

Tags: , , , ,
Filed under Scripts/Reg, VMWare | 1 Comment »