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.

Advertisements

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

     

Finally, a first post

I’ve been thinking about what a good first post would be, and I’ve been busy, and then I realized a week or two has gone by since I set this up.  Then I remembered my no frills philosophy.  Hopefully the syntax highlighting will work.

We have a cluster with about 45 hosts that was going to end up with 60 hosts.  Each blade chassis holds 4 blades, so one of the guys had the idea to break it into 4 clusters of 15 hosts each.  In each chassis the blade in slot 1 would be in cluster 1, slot 2 in cluster 2, and so on.  Of course, now when we’re deploying new VMs we needed to know which cluster had the least load.  This is the quickie report to show the least loaded cluster.  I don’t normally use write-host, but it was the quickest way to get the data.  Later I could integrate this into our VM deployment if it seems worthwhile.

How I used it (up arrow and enter to run it again):

write-host`nCluster`t        # Hosts`tCpuUsageMhz`tMemoryUsageMB`t# VMs`n—————`t——-`t———–`t————-`t—–; get-cluster cluster* | sort | % { $x=get-vmhost-location$_ | measure-Property cpuusagemhz,memoryusagemb -average; write-host$($_.Name)`t  $($x[0].Count)`t  $([int]$x[0].Average)`t`t  $([int]$x[1].Average)`t$((Get-VM -Location $_).Count) }

Readable version:

write-host`nCluster`t        # Hosts`tCpuUsageMhz`tMemoryUsageMB`t# VMs`n—————`t——-`t———–`t————-`t—–get-cluster cluster* | sort | % { 
    $x=get-vmhost-location$_ | measure-Property cpuusagemhz,memoryusagemb -averagewrite-host$($_.Name)`t  $($x[0].Count)`t  $([int]$x[0].Average)`t`t  $([int]$x[1].Average)`t$((Get-VM -Location $_).Count)
}