Get-view, list viewtypes, filter usage, Get-VIObjectbyVIView, and get-vm in powercli

From time to time i check my blog to see how people are getting to my blog 😉 Today i thought that if so many of you are trying to find this information although for some reason it is not in that post to which you are being redirected, i will create a new post about it. So that you will not be disappointed that the information is not there.
So few things about get-view. I think that VMware offers a GREAT documentation so why not take their explanation 😉

The vSphere PowerCLI list of cmdlets includes the Get-View and Get-VIObjectByVIView cmdlets, which enable access to vSphere PowerCLI views from .NET.

Using the vSphere PowerCLI views cmdlets for low-level VMware vSphere management requires some knowledge of both PowerShell scripting and the VMware vSphere APIs.

Continue reading

Advertisements

Change ESXi server license to evaluation while connected to esxi directly using powercli

In order to change license to the evaluation one when the host is still not connected to Virtual Center we can use this approach.

connect-viserver esxi1.local
$lm = Get-View -Id 'LicenseManager-ha-license-manager'
$lm.UpdateLicense("00000-00000-00000-00000-00000", $null)

This will change esxi licene to the evaluation one on Esxi 5.

Locate vms that are in particular cluster and are using particular datastore in powercli

A quick one :
Trying to find which vms are residing on datastore :”datastore_name” and are located in cluster “your_cluster”

Get-Cluster "your_cluster"|Get-vm |?{($_.extensiondata.config.datastoreurl|%{$_.name}) -contains "datastore_name"}
or
Get-Cluster "your_cluster"|Get-vm |?{($_.extensiondata.config.datastoreurl|?{$_.name -eq "datastore_name"})}

It’s easier if the vms reside on particular datastore and you do not care in which cluster they are:

(Get-Datastore -Name 'datastore_name').Extensiondata.Vm|%{(Get-View -Id $_.toString()).name}

And approach where you want to check all datastores in specific cluster:
So i was thinking about this and came out with that 😉 :

get-vmhost -Location "Cluster_01"|get-datastore|%{$ds=$_; $ds.Extensiondata.Vm|%{$_|select @{n='vm name';e={(Get-View -property name -Id $_.toString()).name}},@{n='ds name';e={$ds.name}}  }}

In case you want to check serveral datastores with known names for vms you can do this, assuming that for example names are written in file : c:\dsfile.txt

Get-Datastore -Name (get-content c:\dsfile.txt) | %{$temp=$_; $temp.extensiondata.vm | % { $_ | select @{n='VMName';e={(get-view -property name -id $_).Name}},@{n='ds';e={$temp.name}}}}

Report for all vms regarding uuid,network card, macaddress, ipaddress, network card type e1000/vmxnet in a csv ;)

Right,
i wanted to see all vms, with their current ips, macaddresses, names, networkcard types.
This report should also have all network cards in separate columns in csv. So i needed to dynamically adjust number of those ‘columns’.
Below script will output only ipv4 addresses without ipv6.

$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine  |Sort-Object -Property {  $_.Config.Hardware.Device |  where {$_ -is [VMware.Vim.VirtualEthernetCard]} |  Measure-Object | select -ExpandProperty Count} -Descending 

foreach($vm in $vms){
  $reportedvm = New-Object PSObject
  Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
  Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress  
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

$reportedvms|Export-Csv c:\myreport.csv

Ok, and what if somebody doesn’t want those multiple columns, but wants to have only 1 column that will describe all network cards for particular vm ?

$vms=get-view -viewtype virtualmachine
$(
foreach($vm in $vms){
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
""|select  @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net  info";e={[string]::join(',',  $($networkcards|%{$devid=$_.DeviceConfigId;[string]::join(',',$(($vm.config.hardware.device|?{$_.key  -eq $devid}).gettype().name,$_.network,($_.ipaddress -join ';'),$_.Macaddress))})  )}}
}
)|export-csv c:\myreport.csv

I know 😉 a lot of joining.
@Update: Luc has noticed my error there that i was not sorting $vms array. We have to sort it by the vm with most network cards so that export-csv will work properly.
@Update: Thanks Steve for the feedback. I have updated the code in both examples.
27/02/2014@Update : Fixed issue with reporting multiple ip addresses in second example.

Searching for cdrom devices with Emulate Ide or Passthrough IDE mode in powercli

Right,
so we want to find all vms that have their cdrom device set to Emulate Ide mode.

$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
 if ($vm.Config.Hardware.Device |? {$_.backing -is [vmware.vim.VirtualCdromRemoteAtapiBackingInfo]}) {$vm.name}
}

If we want to look for vms with cdrom devices mode set to Passthrough:

$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
 if ($vm.Config.Hardware.Device |? {$_.backing -is [vmware.vim.VirtualCdromRemotePassthroughBackingInfo]}) {$vm.name}
}

No fancy output 😉 Just sending to screen affected vm names.

‘Cannot complete the HA configuration’ yellow popup message via powercli

I have noticed some day that issue like on screenshot below is not going to the Alert tab. 99% that some other alert created this message, for example host is loosing uplink will result in having this popup to show up. I was wondering how it is possible for me to monitor those yellow popups in summary tab 😉

The easiest way to get information about this popup is to check the configissue property of vmhost object.

$myhost=get-vmhost 'myhost'
$myhost.extensiondata.configissue
-
Message              :
Reason               : agentFailed
Key                  : 0
ChainId              : 0
CreatedTime          : 8/9/2012 12:27:41 PM
UserName             :
Datacenter           : VMware.Vim.DatacenterEventArgument
ComputeResource      : VMware.Vim.ComputeResourceEventArgument
Host                 : VMware.Vim.HostEventArgument
Vm                   :
Ds                   :
Net                  :
Dvs                  :
FullFormattedMessage : HA agent on myhost in cluster X in Y DC has an error :  HA agent on the host failed
ChangeTag            :
DynamicType          :
DynamicProperty      :

That’s pretty much it 😉 In this object there will be FullFormattedMessage property that will describe current issue. From here is now easy to write some little report that will inform us if we have any config issues on our hosts.

VMworld 2012 – checked!

I haven’t been writing any posts for some time now as i was quite busy. Some work, some vmworld… 😉

I also had some tiny vacation period 😉

I wanted to say hi to all people that i’ve met during VMworld 2012. It was great to meet all of you. How was vmworld 2012 in Barcelona ? It was great 😉 Can’t describe it in detail, you have to go there at least once in your life i guess just to see how it looks like 😉 and it looks BIIIIIIIIIIIIIIIIIIG 🙂
So that’s for some explanation why i was not posting anything new. Back to work now !