Archive

Archive for the ‘Virtualization’ Category

PowerShell: Expand virtual machine hard disk size in Vmware ESX

November 22, 2011 Leave a comment

Expanding the Virtual machine hard disk is most common administration task for VMware(or Hyper-V) administrator. The general process is connecting to virtual center console, locate the VM, go to properties, identify the hard disk for which you want to increase the space and enter new hard disk size in the “Provisioned Size” box and click OK. This complete the task. For example, you have 10 such VMs on which you have to increase the space, then repeating the above steps for all VMs is a cumbersome task. So, let us see how we can easily do it with powershell.

Today I got a requirement to expand a VM hard disk. Since I am exploring VMware PowerCLI these days, I thought of using PowerShell for this operation to see how quickly I can do this. Once I figureout the cmdlet to use, I felt it is too easy.

Here is the one liner I used to expand hard disk 1 in a VM.

Get-VM VMServer1 | Get-HardDisk | ? {$_.name -eq "Hard disk 1" } | Set-HardDisk -CapacityKB 52428800

You need to execute this from VMware PowerShell CLI where Get-VM, Get-Harddisk cmdlets are available. Also before executing the above, you need to ensure that you already established a connect to Virtual Center Server from your powershell window. You can do it by simply running “Connect-VIServer -Server VCSERVER1″ where VCSERVER1 is the name of the virtual center server.

In this one liner, I am using Get-VM to get a virtual machine reference for which I want to increase the space and using Get-Harddisk cmdlet to read the disks information of the returned VM. From there I am filtering out the disks for which I want to expand the space. Since I am just interested in expanding “Hard disk 1″, I created a where-object to filter the disks with the given name. And finally expanding the disk by using set-HardDisk cmdlet which takes the target size in KBs.

From my readings, I understood that, Set-Harddisk also expands the disk in guest OS provided you are passing the credentials to it. I did that for a VM which is running windows 2003 but it didn’t work. May be this is applicable only to Windows 7/2008 computers which are having in-built GUI option to extend the hard disks from disk management. No need to use disk part tool in that case. If it is a windows 2003/XP guest OS, then you should use diskpart tool to expand it.

Hope this information helps…

 

PowerShell: Find out who else logged on to a VM console using virtual center

November 17, 2011 Leave a comment

Often we see below message when we try connecting to a console of virtual machine using Virtual Center MMC.

“Number of Active Connections has changed. There are now X active connections to this console.”

All it indicates is someone else is also sharing the console. Most VMware admins found it frustrating because, it is diverting the attention and even if you close it, it will pop-up again. Looks like there is no option to disable this message. Read http://communities.vmware.com/thread/81825 if you need more details about the problem.

Since there is no way available around to disable that, can we have some workaround? One thing we can do is ask the persons who are currently connected to disconnect their sessions. But the question here is “how we know who all are connected”? There is no straight forward option in GUI to find out the users, but you can use VMware powershellCLI to get this information.

 

function Get-VMConsoleConnectedUser {
param (
$VmName,
$VCName
)            

Connetct-VIServer -Server $VCName
$VMEntity = Get-VM $VMName
Get-VIEvent -Start (Get-Date).addhours(-5) -Enity $VMEntity | ? `
{$_.FullFormattedMessage -like "Remote Console*"} | select Username,FullFormattedMessage, createdtime            

}

A few things you should keep in mind before using the above function:

  • You should execute this from a powershell console where PowerCLI add-in is available
  • This function is not having any error handling capabilities
  • By default script looks for remote console logins in last 5 hours. Change the (get-date).addhours(-5) section if you would to change it.
  • Always test it before you run in production

This is my first attempt to manage VMware Virtual center with powershell.

Hope this article helps.

 

VMWare Powershell(CLI) Poster that you should have at your desk

September 16, 2011 Leave a comment

I came across a nice VMWare powershell poster that every VMWare admin should have at desk. It is a easy and easy reference for every Vmware administrator.

The new poster adds to the original vSphere PowerCLI core cmdlets and allow you to quickly reference cmdlets from the following :

  • vSphere
  • Image Builder
  • Auto Deploy
  • Update Manager
  • Licensing
  • View
  • vCloud

This poster can be downloaded from http://communities.vmware.com/thread/327234

Happy learning…

How to force reset a guest VM in ESX environment

Often we fall into a situation where resetting VM from Virtual Center console won’t help and server remains hung. In such cases we need to “hard” reset the VM to get it back online. 

To hard reset a VM, there are two options…

Option 1:

  1. SSH to host as root where VM is hosted
  2. Issue “vmware-cmd -l” to list the guests on the current host
  3. In output you will see VMX file location for each guest
  4. Now issue “vmware-cmd <path_of_guest_vm_vmx_file_that_you_want_to_restart> stop hard”
  5. If above command is successful, start the VM either from VC console or with command “vmware-cmd path_of_guest_vm_vmx_file_that_you_want_to_restart> start” 

Option 2:

In most cases, it option#1 should work. If it throws errors for some reason, try below method.

  1. SSH to host as root where VM is hosted
  2. Issue “vm-support -x”
  3. Result will give vmid(s) of guests running on the current host
  4. Now issue “vm-support -X 1234” where 1234 is the vmid of guest which you want to force reset
  5. Answer all the questions — mostly yes for all, but use your won intelligence
  6. This process will generate some debug information, so I prefer to execute this command from /tmp location.
  7. Once execution is completed, either use step#5 in option#1 or Virtual Center to start your guest VM.

Hope this helps…

Categories: Virtualization Tags: