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.

Advertisements

Get-LicenseReport to obtain informations regarding licenses within Virtual Center

If you need to create reports regarding licenses usage you can the below script. It gathers all licensing information for virtual center to which you are currently connected. Before running this function make sure you are connected to one single virtual center instance. Please run this function within PowerCLI console, as for now i haven’t put check/loading vmware snappin

Function Get-LicenseReport
{
  <#

   .Synopsis

    Get licensing report from currently connected Virtual Center

   .Description

    This function returns licensing information from the currently connected 
	Virtual Center server. It can output to screen or to html file using the
	ConvertTo-HTML option.
	PreContent of html report can be modified in source code.
	Variable $PreContent describes what is written before generating 
	the table.

   .Example

    Get-LicenseReport

    Will create licenses usage report from currently connected VirtualCenter
	server and output to screen

   .Example

    Get-LicenseReport -htmlfile "C:\licensereport.html"

    Will create licenses usage report from currently connected VirtualCenter
	server and output to file "C:\licensereport.html"
	
	Get-LicenseReport -htmlfile "C:\licensereport.html" -Name "Grzegorz 
	Kulikowski" -Email "myemail@web.com" -CustomerNumber "123123"

    Will create licenses usage report from currently connected VirtualCenter
	server and output to file "C:\licensereport.html" and fill out predefined
	field within report such as name, email, vmware customer number

   .Parameter CustomerNumber

    If you need to specifi additional field like Customer Number
	
	.Parameter Name

    If you need to specify name of the person who creates the report
	
	.Parameter Email

    If you need to specify e-mail of the person who creates the report
	
   .Parameter htmlfile

    Specifies the file name to which html version report will be written

   .Notes

    NAME:  Get-LicenseReport

    AUTHOR: Grzegorz Kulikowski

    LASTEDIT: 08/13/2012
	
	SPECIAL THANKS: Luc Dekens, Robert van den Nieuwendijk, Yasen Kalchev
	CSS TABLE: http://www.textfixer.com/resources/css-tables.ph
	
	NOT WORKING ? #powercli @ freenode.irc.net / 

   .Link

    https://psvmware.wordpress.com

 #>
param (
[string]$htmlfile,
[string]$CustomerNumber,
[string]$Name,
[string]$Email
)
$Head=@"
<Title>License Report for $($global:DefaultVIServer.name)</Title>
 <Style type="text/css">
  P.myinfo {text-align: Left}
  body {
   margin-left: 20%;
   margin-right: 20%;
   padding-top: 30px;
   padding-bottom: 40px;
   color: #2E2E2E;
   background-color: #E6E6E6 }
   
  table.gridtable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #666666;
	border-collapse: collapse;
}
  table.gridtable th {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #dedede;
}
  table.gridtable td {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #ffffff;
}
 </Style>
"@
 
$PreContent=@"
<center>
<p><h1>License Report</h1></p>
<p class=myinfo>VMware License Review – for VC:<b> $($global:DefaultVIServer.name) </b></br>
Customer Number:<b> $CustomerNumber </b></br>
Name of Person completing this form: <b>$Name</b></br>
Email of Person completing this form: <b>$Email</b></br>
Date: <b> $(get-date) </b></br></br</p>
"@

if(-not ($global:DefaultVIServer) ) { 
	Write-Host -ForegroundColor Red -BackgroundColor Black "You are not connected to any VirtualCenter server. Please connect manually."
	Write-Host -ForegroundColor Yellow -BackgroundColor Black "hint: Connect-VIServer -server myVC.domain.biz -Credential (Get-Credential)"
	break
}
$param = @($null)
$ServiceInstance = Get-View ServiceInstance
$LicenseManager= Get-view $ServiceInstance.Content.LicenseManager
$LicenseAssignmentManager= Get-View $LicenseManager.LicenseAssignmentManager
$LicensesArrayList=New-Object System.Collections.ArrayList 
$LicenseAssignmentManager.GetType().GetMethod("QueryAssignedLicenses").Invoke($LicenseAssignmentManager,$param) |Foreach-Object{
$License=New-Object -TypeName PsObject -Property @{ProductName=$null;Version=$null;Edition=$null;DisplayName=$null;PhysProcessors=$null;CoresPerCpu=$null}
$License.ProductName= ($_.Properties.GetEnumerator()|?{$_.Key -eq 'ProductName'}).Value
$License.Version= ($_.Properties.GetEnumerator()|?{$_.Key -eq 'ProductVersion'}).Value
$License.Edition= $_.AssignedLicense.Name
$License.DisplayName=$_.EntityDisplayName
    if($_.AssignedLicense.EditionKey -like "vco*"){
			$License.DisplayName+=" Used: $($_.AssignedLicense.Used) from: $($_.AssignedLicense.Total)"
		}
	if($_.EntityId -like "host-*"){
		$HostView=Get-View -Id  "HostSystem-$($_.EntityID)"
		$License.PhysProcessors=$HostView.Hardware.CpuInfo.NumCpuPackages
		$License.CoresPerCpu=$hostview.Hardware.CpuInfo.NumCpuCores/$HostView.Hardware.CpuInfo.NumCpuPackages
	}
		else {
			$License.PhysProcessors="N/A"
			$License.CoresPerCpu="N/A"
		}
$LicensesArrayList.Add($License)|Out-Null
}
if($htmlfile){
	($LicensesArrayList | Select-Object ProductName,Version,Edition,DisplayName,CoresPerCpu,PhysProcessors | ConvertTo-Html -PreContent $PreContent -Head $Head ) -Replace '<table>','<table class="gridtable">' |Out-File $htmlfile
	}
	else{
		return $LicensesArrayList
	}
}

I would like to thank Luc Dekens, Robert van den Nieuwendijk, Yasen Kalchev for their support while i was trying get all the information which was needed in order to obtain all data from Virtual Center, as this one was bit tricky 😉

get-licensereport image

get-licensereport image


While writing this script it was very difficult to pass $null to QueryAssignedLicenses method, i mean it was impossible ;). Why did i have to query like that ?

$LicenseAssignmentManager.GetType().GetMethod("QueryAssignedLicenses").Invoke($LicenseAssignmentManager,$param)

http://communities.vmware.com/message/2096977#2096977 Yasen Kalchev explains why do we have to use this approach in order to get ALL licensing information from virtual center.
By the way it’s very easy to obtain exactly the same result if you will point your web browser directly to
https://your-vc-address.biz/?moid=LicenseAssignmentManager&method=queryAssignedLicenses
And leave the EntityID parameter as empty.
What i have also learned while writing this script is that EntityID for VC is kept in

(Get-View ServiceInstance).Content.About.InstanceUuid

And for all esx/i hosts the id is the Value of its MoRef for example HostSystem-host-123 . So EntityID in this case would be the ‘host-123’ part.
If you want to change the content of the html report just edit contents of PreContent/Head variables. If you want to change the look of the html report , you have to edit css style. If you will modify the css table style, make sure you will also change the style name in the -replace section as well, because by default ConvertTo-Html creates a table tag without any style.
Enjoy!

:Update
I have added functionality to get-LicenseReport. If it will discover VC OPS, it will also put into displayname TOTAL and USED numbers for that license.

ESX/ESXi host in vCenter Server reports the error: Unable to apply DRS resource settings on host. The operation is not allowed in the current state

What helped in my situation:
service mgmt-vmware restart
on the service console
Official KB for this situation:
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1028351

Clone roles between two virtual center servers

So i wanted to make a copy of a role that is in vc1. I wanted to have it in vc2.
Basically you can just open two vSphere clients, one to vc1 and other to vc2, and just select each privilege that is in this role and select it on second window. (click,click,click…) Yeah, if you have 1 role to copy, with very small amount of privileges then it is not such issue. But what if you have more?
Ok, let’s start from the beginning.

Sitauation:
VC1 ---|
.      |
.    RoleA

VC2 ---|
.      |
.    RoleA

We want to copy roleA from VC1 to VC2.
First check if your powercli runs in multi VC mode.

PowerCLI C:\Get-PowerCLIConfiguration

Proxy Policy    Default Server
                Mode
------------    ---------------
UseSystemProxy  Multiple

If not, set the default server mode to multi.

Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false

Once this is set you can now connect to multiple VC servers. Since -server handles array we can do:

Connect-viserver -server "VC1","VC2" -credential (get-credential)

Role that will be cloned has name RoleA within VC1, let’s view it:

Get-VIrole -Name "RoleA" -Server VC1 | fl *

We can see it’s description,Name,ID and what will be most important for us PrivilegeList
Let’s store the privilege list for this roleA from VC1. We will store those privileges ids as a string in string array.

[string[]]$privsforRoleAfromVC1=Get-VIPrivilege -Role (Get-VIRole -Name "RoleA" -server VC1) |%{$_.id}

Once we have the privileges ids, we can now create blank role in VC2.

New-VIRole -name "RoleA" -Server VC2

We will now populate privileges in our empty RoleA within VC2:

Set-VIRole -role (get-virole -Name "RoleA" -Server VC2) -AddPrivilege (get-viprivilege -id $privsforRoleAfromVC1 -server VC2)

If everything went fine we should have now 1:1 copy of our RoleA. Let’s check it:

(Get-VIRole -Name RoleA -Server VC1).PrivilegeList.Count
(Get-VIRole -Name RoleA -Server VC2).PrivilegeList.Count

If you don’t want to use this additional variable that holds privileges you can put it directly in one line

Set-VIRole -role (get-virole -Name "RoleA" -Server VC2) -AddPrivilege (get-viprivilege -id (Get-VIPrivilege -Role (Get-VIRole -Name "RoleA" -server VC1) |%{$_.id}) -server VC2)

That’s it 😉

Get-drsrule enhancement

I was doing some drs rules today and noticed( after all those years 😉 ) that i get only id’s for vms in output. So i can not see directly which vms for example are set to be together etc…
In order to quickly have the output with the names instead of ids i wrote this tiny enhancement:

get-cluster XXX | Get-DrsRule | select Name, KeepTogether,Enabled, @{N="VMnames";E={ $_.Vmids|%{(get-view -id $_).name} } } 

If the output is not optimized you can always use |ft -autosize at the end of the line.
For each of the virtual machine id i get the vm view and take its name.
You can do get-vm -id or get-view -id. If there is small amount of vms in your rules, that would not affect any performance of this command.
Tiny enhancement but saves some time on checking those ids manually.

Migrating virtual center database [oracle]

I was doing some checks today and was surprised that i have few services within vCenter Service Status that have Alert status. I forgot to take screenshot from it:/
They were in the “License Services” section.

When you are migrating database like i did, from 1 host to another and you have edited the:
C:\app\Administrator\product\’version’\client_1\network\admin\tnsnames.ora
and have new information like:
NAME_HERE =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = NEW_HOST )(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = XXXX )
)
)

Then after checking the right TNS Service Name in Data Sources (ODBC) and starting your VC, i assumed everything is fine.
I missed one thing as it seems. The Services with alerts in vCenter Service Status, were red because i did not change 1 config file which is:
“C:\ProgramData\VMware\VMware VirtualCenter\vcdb.properties”
You will see that old db hostname is still there:
usevcdb=true
url=jdbc:oracle:thin:@//OLDHOSTNAME:1521/old_name
dbtype=oracle
driver=oracle.jdbc.OracleDriver

You need to replace the old hostname with the new one there. After this , restart your vpxd and check again vCenter Service Status.