Cloning vsphere annotations

Just a sneak peak of one from examples that i will be presenting this year on PLVMUG meeting.
Cloning vm annotations from source vm, selecting annotations that should be cloned, applying it to destination vms.After PLVMUG 2014 session, source code will be available.(i think 😉 )

Advertisements

Receving cluster name for vms fast.

If there is built in mechanism of getting the clustername for VM then i don’t know it 😉 that’s why i wrote this idea down. Well it does the trick, that’s what counts for me. So here is the idea that i drew in paint 😉
drawing

So we will make hashtables for VMs to VMHosts, then VMHosts to cluster names, and then get the cluster name for vm by choosing the correct hash.
I don’t know if i am explaining that clearly, so please don’t hesitate to write comments.

$Clusters=get-view -ViewType ClusterComputeResource -Property Name,Host
$vms=Get-View -ViewType VirtualMachine -Property Name,Summary.Runtime.Host
$VMs|%{ [hashtable]$VmsToHostHash+=@{ $_.Name.ToString() = $_.Summary.Runtime.Host.ToString()}}
foreach ($cluster in $clusters ) {$cluster.Host | % { [hashtable]$HostToClusterHash+= @{$_.ToString()=$cluster.Name}} }

We receive first clusters objects. We will use their Name property and Hosts that are inside. Then We get the virtualmachines objects with name and host on which it is living information. After this we are building the hashtables that are mentioned earlier.

We could just simply use it by calling:

$HostToClusterHash[$vmsToHostHash['VMNameGoesHere']]

That can be all, but we can write little simple function to query that as well. Of course that could be built in proper manner, with some error handling and getting all kinds of objects and not string only.

Function Get-ClusterNameFromVMName{
param(
[string[]]$VMName,
[hashtable]$VmsToHostHash,
[hashtable]$HostToClusterHash
)
return $HostToClusterHash[$vmsToHostHash[$VMname]]
}

We could then get cluster name by typing:
Get-ClusterNameFromVM -VMName ‘SomeVMname’ -VmsToHostHash $VmsToHostHash -HostToClusterHash $HostToClusterHash

Basically the first part that is collecting needed data should run pretty fast, for example on VC with ~2000 vms it completes within 4.5 second.
The part to get actually the cluster name is instant.

While getting data we also prepared the $vms variable that holds vms objects so if we would do now:
measure-command {foreach ($vm in $vms){ $vm | select name, @{n=’CLname’;e={$HostToClusterHash[$vmsToHostHash[$($vm.name)]]}} } }

we can expect below results:
Seconds : 0
Milliseconds : 351

Output would be very simple:
VMName ClusterName

This is just an idea, you can try to use this approach whenever you need to quickly get just cluster name for VM. What is worth adding 😉 This is not perfect solution, you have to remember that hashtables can refuse to add vm if you have vms with duplicate names. The main reason for me to build this is that i will be doing some examples for plvmug that will be querying a lot vms in regards of their clusters. This script only watches cluster, not single vmhosts, so please have this in mind as well.

[update]
As per Robert van den Nieuwendijk comment. Actually there is also more elegant way of getting the cluster name for vm. Below is example from Robert:

Get-VM | Select-Object -Property Name,@{Name=’Cluster’;Expression={$_.VMHost.Parent}}

#And with new-viproperty approach

New-VIProperty -ObjectType VirtualMachine -Name Cluster -Value {$Args[0].VMHost.Parent} -Force
Get-VM | Select-Object -Property Name,Cluste

Getting Active Directory users thumbnail picture to disk with powershell

I am writing some examples now to PLVMUG presentation. Long story short, i wanted to get the AD thumbnail picture saved to disk. Spent 20-30 minutes on google , but only thing i was getting was how to upload the picture to Active Directory 😉 Well, after 20 mintues i decided to focus on writing it in that case.

$ADUser = (([adsisearcher]"Samaccountname=SomeUser").findone()).Properties
[byte[]]$imgdata = $ADUser.thumbnailphoto | %{ $_ }
$img = [System.Text.Encoding]::Default.GetString($imgdata)
Set-Content -path c:\myimage.jpeg -Value $img

So if we have uploaded before a picture to a user we should be able to save it now to C:\myimage.jpeg. Now the other thing that i was trying to achieve is to put that image to a picturebox object. So just for reference i will put it here as well. This way of injecting image to picturebox is not using files, instead it can read the image directly from variable like $img, from previous example. Found a solution here.

$ADUser = (([adsisearcher]"Samaccountname=SomeUser").findone()).Properties
[byte[]]$imgdata = $ADUser.thumbnailphoto | %{ $_ }
$image=[System.Drawing.Image]::FromStream([System.IO.MemoryStream]$imgdata)
$picturebox1.Image= $image

Enjoy!

PLVMUG meeting, Warsaw – May 13th 2014

plvmugs
Please feel invited to PLVMUG meeting taking place on May 13th in Hotel Westin, Al. Jana Pawła II 21, Warsaw, Poland. I was asked to give a speech about PowerCLI. I hope you will enjoy it. This session starts at 12:50.

You can sing up by clicking here.
Agenda:
10:00 Przywitanie Gości
10:15 Backup to dopiero początek. Myśl kompleksowo, działaj wzorcowo – Simpana 10, Jerzy Janowski, CommVault
11:00 Licencjonowanie produktów Oracle na platformie VMware vSphere, Krzysztof Waszkiewicz, VMware
12:35 Przerwa kawowa
12:50 PowerCLI w przykładach, Grzegorz Kulikowski
13:40 Obiad
14:40 EMC i VMware: prawdopodobnie najlepsza kombinacja na świecie. W świetle fleszy ŻMiJA, Xtremalnie skalowalne IO i inne nowości prosto z EMC World, Karol Boguniewicz, EMC
15:25 Pamieć operacyjna w systemie VMware ESXi, Michał Gąsior, Bank Zachodni WBK
16:15 VMware NSX, Maciej Lelusz, Inleo
17:00 Rozlosowanie nagród oraz zamknięcie spotkania

See you there !