Duncan recently posted a nice license script which checks every ESX Host for the availability of the license source, in his post he challenged me to write a script which checked the state of the license server service and sent an email if it was not running.
How could I let this pass without an answer, so I finally found time, the below script will get the license server from the VI server and then check to see if the license server service is running, if its not it will drop you an email, the idea of this is to run it as a scheduled task.
The account you run this scheduled task as will need to be an admin on the license server for the pass through credentials to work for the WMI call.
Function EmailFile () {
$smtpServer = “mysmtpserver.com“
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From= “me@mydomain.com“
$msg.To.Add(“me@mydomain.com“)
$msg.Subject= “VMware License Server Check“
$msg.Body= “The VMware license Server service is not in a running state, please check it!“
$smtp.Send($msg)
}
Connect-VIServer MYVISERVER
$ServiceInstance = Get-View ServiceInstance
$LicenseMan = Get-View $ServiceInstance.Content.LicenseManager
$LicenseServer = (($LicenseMan.Source.LicenseServer).split(‘@‘))[1]
$State = get-wmiobject win32_service -ComputerName $LicenseServer -filter “Name=’VMware License Server’“ |Select State
if ($State.State-ne “Running“){
EmailFile
}
While I was at it I thought I may as well include a nice script which will tell you what your hosts are licensed for, this can be run against a cluster/host or all hosts in your virtual infrastructure.
Connect-VIServer MYVISERVER
#The next line can be used to define a cluster of hosts or all hosts or a single host
$ESXHosts = Get-VMHost |Get-View
$ServiceInstance = Get-View ServiceInstance
$LicenseMan = Get-View $ServiceInstance.Content.LicenseManager
$Query = $LicenseMan.QueryLicenseUsage
foreach ($ESXHost in $ESXHosts)
{
$LicUse = $LicenseMan.QueryLicenseUsage($ESXHost.MoRef)
Write-Host -ForegroundColor Yellow $ESXHost.Name
Write-Host “License Server: $($LicUse.Source.LicenseServer)“
foreach ($Reservation in ($LicUse.ReservationInfo |Sort Key)){
If ($Reservation.State-eq “licensed“){
switch ($Reservation.key)
{
“esxFull“ {$FriendlyName = “ESX Server Standard“; break}
“esxExpress“ {$FriendlyName = “ESX Server Foundation“; break}
“backup“ {$FriendlyName = “VMware Consolidated Backup Usage“; break}
“vmotion“ {$FriendlyName = “VMotion“; break}
“drs“ {$FriendlyName = “VMware DRS“; break}
“das“ {$FriendlyName = “VMware HA“; break}
“esxHost“ {$FriendlyName = “VirtualCenter Agent for ESX Server“; break}
“nas“ {$FriendlyName = “NAS Usage“; break}
“iscsi“ {$FriendlyName = “ISCSI Usage“; break}
“san“ {$FriendlyName = “SAN Usage“; break}
“vsmp“ {$FriendlyName = “Up to 4-way SMP“; break}
default {$Friendlyname = “Feature not yet in script“;break}
}
Write “– $FriendlyName licensed for $($Reservation.Required) Processors“
}
}
}
Pingback: PowerCLI: vSphere License Export « Virtu-Al
Hi Alan
I have replied back using my work address @adobe.com. Let’s use this account to communicate going forward.
Thanks for all your help !
@DJLO
Ok, I will drop you an email
Hi Al
I have tried running the following script from vSphere PowerCLI 2 ways, 1 by running it and 1 by executing Connect-VISERVER 10.36.206.10 and i get this error both ways
[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts> get-wmiobject win32_service -ComputerName “10.36.206.10” -filter “Name=’VMware License Server'”
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:14
+ get-wmiobject <<<
@DJLO
Ok, lets start with the basics, from a powershell prompt are you able to get a result from the following:
get-wmiobject win32_service -ComputerName “MyLicenseServer” -filter “Name=’VMware License Server’”
Obviously replace the MyLicenseServery with the hostname of your license server.
Dropped you an email RE:customized reports
Hi Al
Thanks again for the help. I have 3 accounts that are members of the Administrator group on my VC server, myown, administrator and lmadmin
When i try the script with all 3 accounts i keep getting the access denied error. I am able to RDP into the VC machine with any of those 3 accounts.
Also we are curious if you do any consulting on the side. We are looking to get some customized reports for our VC and Lab Manager environment. My boss is quite impressed with the scripts i found from you. Is there a way i can contact you to discuss this further?
@DJLO
From the ‘access is denied’ error you are getting I would say your account you are running it as does not have permissions to the virtual center server. You will need access for the script to make a WMI call.
Welcome to PowerShell, your life will never be the same again !
Hi Al
Wow thanks so much for the super fast reply. I have just discovered powershell 2 days ago ( I must have been living under a rock, this is awesome)
I am now getting a different error
[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scrip
ts> ./liccheck
Name Port User
—- —- —-
10.36.206.10 443 administrator
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS
DENIED))
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\liccheck.ps1
:24 char:23
+ $State = get-wmiobject <<<< win32_service -ComputerName $LicenseServer -filt
er “Name=’VMware License Server'” | Select State
You cannot call a method on a null-valued expression.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\liccheck.ps1
:15 char:13
+ $att.Dispose( <<<< );
@DJLO
I have just adjusted the script, can you try the new version please.
Thanks
I am getting the following error when trying to run the license check
You cannot call a method on a null-valued expression.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\liccheck.ps1
:23 char:80
+ $LicenseServer = ($LicenseMan.Source.LicenseServer).Substring($original.Index
Of( <<<< “@”)+1)
Get-WmiObject : Cannot validate argument because it is null.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\liccheck.ps1
:24 char:51
+ $State = get-wmiobject win32_service -ComputerName <<<< $LicenseServer -filt
er “Name=’VMware License Server'” | Select State
You cannot call a method on a null-valued expression.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\liccheck.ps1
:15 char:13
+ $att.Dispose( <<<< );