Richard Parmiter

Virtualisation blog and Knowledge Base

  • You are here: 
  • Home
  • script

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 »

use xe.exe to start/shutdown vm’s on XenServer

Posted on July 27th, 2010

I wanted to stress test a Citrix XenServer 5.6 computer to see how it copes with a number of simultaneou startup and shutdown requests.

I also wanted to see the IOPS passed to back end storage where the virtual machine hard drives are stored.

Rather than manually selecting the machine in XenCenter and shutting down or starting I wrote a script to perform the same action which made it a little quicker and easier when performing this task many times.

I wanted to perform the actions on a specific list of vm’s listed in a text file

List of VM’s – vm.txt

Name-of-VM1

Name-of-VM2

Name-of-VM3

Remember they are case sensitive and the same name you see in XenCenter. It can also be acheived with UUID’s of the VM’s but this is less friendly, so the names were used.

Startup VM’s

This is a simply dos batch script

set XenServer=[server name or IP]
set XenUser=[username]
set XenUserPwd=[password]

for /F %%1 in (vm.txt) do (
start “start %%1” “c:\program files (x86)\Citrix\XenCenter\xe.exe” -s %XenServer% -u %XenUser% -pw %XenUserPwd% vm-start name-label=%%1
)

I have used the start command to open a seperate dos box for each VM so they are done in parallel. Without this the command waits for each VM to start before starting the next one. Running them all in parallel is *much* better for a stress test of a boot storm simulation!

Shutdown VM’s

set XenServer=[server name or IP]
set XenUser=[username]
set XenUserPwd=[password]

for /F %%1 in (vm.txt) do (
start “shutdown %%1” “c:\program files (x86)\Citrix\XenCenter\xe.exe” -s %XenServer% -u %XenUser% -pw %XenUserPwd% vm-shutdown name-label=%%1
)

I will no doubt be working on some updates to these scripts in the near future and will share them soon.

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

Profiling Adobe Acrobat Reader 9.2

Posted on October 21st, 2009

In order to profile Adobe Acrobat Reader 9.2 for Citrix streaming I did the following.

Stage 1 – Download source files

Download and install the Adobe Customization wizard 9

Download the distribution version of Acrobat reader 9.2. You need to register on the adobe site to get access to this download.

Extract the msi from the .exe that was downloaded by running the following

AdbeRdr920_en_US.exe” -nos_ne -nos_o”Extracted”

This will place the extracted files in the “Extracted” folder

Stage 2 – Create a customised installation

Open the customization wizard

Open the Reader msi (extracted above)

Select the options desired. I chose the following:

Supress display of EULA

Remove acrobat.com features

disbale all updates

disable help | Digital editions

Disable product improvement programs

Disable help | Purchase acrobat

Save the customisation MST file

Stage 3 – install

Create a .cmd file that contains the install string including the MST file. For example:

msiexec /i “acroread.msi” TRANSFORMS=”adobe_9_2_transform.MST”

Launch this .cmd file from the Citrix Streaming Profiler

Install as normal

Stage 4 – Add a pre-launch script

Even though the option to disable updates was selected during the customisation wizard, it is not disabled when streaming the app. The same applies to the customer improvment program.

The best way to remove these is to run a pre-launch script to set the reg keys to remove these options.

Create a .vbs script with the following contents

Set objWshShell = CreateObject(“WScript.Shell”)
objWshShell.RegWrite “HKLM\Software\Adobe\Adobe ARM\1.0\ARM\iCheck”, 0 ,”REG_DWORD”
objWshShell.RegWrite “HKCU\Software\Adobe\CommonFiles\Usage\Reader 9\OptIn”, 0 ,”REG_DWORD”

Call this script as a pre-launch script within the streaming profiler.

..

Tags: , , , , , , , ,
Filed under Citrix XenApp, Scripts/Reg | 3 Comments »

Script variable trimming

Posted on May 8th, 2009

Suppose you are trying to run some scripts based on a command line variable, but the variable being passed to it is surrounded by quote marks.

i.e. “device_name” when all i want is device_name

There is an easy way to trim this. In the script need to set this passed variable to a seperate variable, and then trim it byt doing the following:

SET tempvariable=%1                 (need to set %%1 if its run as a script file)

echo %1                                      (will show “device_name”)

echo %tempvariable%                 (will show “device_name”)

echo %tempvariable:~1,-1%       (will show device_name)

The trimmed variable can then be used for another task, i.e. to start a service on a remote server

sc \\%tempvariable:~1,-1% start “ServiceName”

Happy trimming!

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