Monthly Archives: August 2012

How I avoid working with the wrong VM, host, server.

At all costs I avoid typing or having to remember names.  🙂

Most of the work on VMs we do is managed by our ticketing system.  I’ve got a directory “Tickets” and for the ones where I’ll need to work with files there’s a sub-directory “Tickets\ticketnumber”.  Say there’s 15 VMs that need a hard drive size increase.  Those 15 VMs will go in a file “servers_ticketnum_comment.txt” with each VM name on one line.  Then all the operations I do will look like “get-content servers_ticketnum_comment.txt | % { do-something $_ }”

That’s the general prinicple.  Put the names of whatever you’re working on in a text file, CSV file, or variable, and keep reusing it.  You could use a Get-VM filter to select the VMs to work with, “get-vm *servertype*”, but if a new VM gets added with a similar name you could start making changes to the wrong VM(s).

It’s also handy if you need to get back to working on the ticket in a week you don’t have to set anything up, just use the file.  If 2 months later there’s a question about which VMs were modified you can check the file.  (It should be logged in your transcript, too.)  If it’s short term and not ticket work I might just use a variable.  Using CSV is for when, using the above example, each hard drive is getting increased by a different size.  The CSV columns would be “VM,SizeKB”.

Looking for a something in a PowerShell object using a text file

I feel like a good chunk of my time writing scripts is not spent writing code, my scripts/one-liners are usually pretty simple, but spent looking through an object for the property I want to report on or change.  At times I’ve spent 15-20 minutes doing Internet searches and long series of:

$> $x = get-vm somename
$> $x | fl *
$> $x | select -expand extensiondata
$> $x | select -expand extensiondata | select -expand guest
$> $x | select -expand extensiondata | select -expand config

and so on.

After doing that for a couple of years I woke up one morning and felt like an idiot.  It occurred to me that all I needed to do is use Export-Clixml to dump the object into an XML file and then search it using your tool of choice.

$> Get-View -VIObject (Get-VMHost -Name esxhostname) | Export-Clixml -Path vmhost_object.xml

Then open vmhost_object.xml with Notepad++, a web browser, search with Select-String, whatever works for you.  If you open the file with a browser then you can collapse/expand nodes and in general just browse around pretty easily.

That’s a whole lot less typing and so much faster.

Copying a role from one vCenter to another

This is just the steps I did.  If you were going to do this a lot it could be put into a script/function.  Something like “Copy-VCRole -SourceVC xxx -TargetVC yyy -Role nameofrole“.  You also could kind of do source control by picking one vCenter to be the “role source server” and copy out the roles to the other vCenters from there.

  1. Connect to the vCenter that has the role you want.
  2. Save the privileges of the role you’re copying.
    1. $Role_OpsConsoleAccess = Get-VIPrivilege -Role “Ops Console Access”
  3. Connect to the vCenter you’re copying the role to.
  4. Create a new empty role.
    1. New-VIRole -Name “Ops Console Access”
  5. Add the privileges to the new role.
    1. $Role_OpsConsoleAccess | % { Set-VIRole -Role “Ops Console Access” -AddPrivilege (Get-VIPrivilege -PrivilegeItem $_.Name) }
  6. Check it.
    1. Get-VIRole “Ops Console Access” | select -expand privilegelist