Category Archives: powershell

PS: One liner to break long string to fixed width

Was working on a KB from VMware and had to pick up a certificate signature string and covert it to PEM format in 64 character width. I am sure that if search hard enough in the net, there are better examples. Basically, it takes in the long strong, grabs 64 characters and puts into a string array and remove them from the original string. This repeats until the string is 64 characters or less

$longstr = gc .\certstring.txt ; $PEMstr = @() ; 
do { 
  $PEMstr += $longstr.Substring(0,64) ; 
  $longstr = $longstr.Remove(0,64) } 
while ( $longstr.length -gt 64 ) ; 
$PEMstr += $longstr

#The above code will convert a single long string to 64 character width


Leave a comment

Posted by on April 2, 2021 in powershell


PS: Get cluster CPU/Memory over-subscription

I am not the originator of these code (sorry I don’t remember where in google land they came from), but am posting it here for reference, since I may need to reuse it someday.

Note: CPU calculation is divided by 2 due to hyper-threading

#get cluster CPU over-subscription
Get-Cluster | Sort name | Select Name, @{N="CpuOversubscriptionFactor";E={[math]::Round((($_|get-VM|measure numcpu -sum).Sum)/(($_|get-vmhost|measure numcpu -sum).sum)/2,2)}}

#get cluster Memory over-subscription
Get-Cluster | Sort name | Select Name, @{N="MemOversubscriptionFactor";E={[math]::Round((($_|get-VM|measure MemoryGB-sum).Sum)/(($_|get-vmhost|measure MemoryTotalGB -sum).sum),2)}}

#get VMHost CPU over-subscription in a cluster
Get-Cluster <name> | Get-VMHost | Sort name | Select Name, @{N="CpuOversubscriptionFactor";E={[math]::Round((($_|get-VM|measure numcpu -sum).Sum)/(($_|measure numcpu -sum).sum)/2,2)}}
Leave a comment

Posted by on May 30, 2020 in powershell



This set of scripts were developed using similar concept as vchecks but for a more real world enterprise usage. Its not that vcheck is not good, but when you are in a big company, having to read a vcheck email to check the status of daily is very tired. vChecks suffers from the following:

  • Lack of over RAG (red/amber/green) status on subject line. I agree that its hard to really determine what should be red or amber. But in an enterprise operations, RAG give a quick overview of your status. So if the subject says green, you really don’t need to read through the email. Having said that, this script doesn’t send any emails, more about this later.
  • You don’t know if a plugin was successfully executed or not. When a vcheck plugin executes, it either produces a select object or doesn’t. Not producing a select object could also mean that script did not execute. For enterprise operations, its essential to know if each health is executed and the status even if its all green.
  • If I need to run vchecks continuously, say every 30 mins or hour, it may not be achievable because in an enterprise setting, it may take a while to complete a full run of vchecks.

This is an alternate version of vCheck-like script written by me, but sending status outputs to a log file instead. This was developed as the use case for vCheck did not fit with a large enterprise operation organisation.

Git hub repo here ->

Leave a comment

Posted by on April 19, 2020 in powershell, Scripts, vmware


PowerCLI: Up time of virtual machines

2 very prominent solutions are offered when you try to search for powerCLI scripts to check up time of virtual machines in your estate

  • $vm.ExtensionData.Runtime.BootTime
  • Get-Stat -Entity $VM -State sys.uptime.latest -RealTime -MaxSample 1

I don’t even know why some people recommends them. It is very obvious from other posts that these values have issues.

$vm.ExtensionData.Runtime.BootTime is not a consistent value, i.e. the value is not there all the time.

whereas, sys.uptime.latest only records the uptime in that host. If the VM is moved to another host, this is reset

It also took me awhile to find the correct solutions and it was quite frustrating because many techies just blindly offer the wrong advise to users.

Anyway, the correct method is this:

Get-Stat -Entity $VM -State sys.osuptime.latest -RealTime -MaxSample 1

You can compare the values on the same VM, yourself

Leave a comment

Posted by on March 22, 2020 in powershell, vmware


vSphere: Enable/Disable DRS Host rules with PowerCLI

I know that the latest version of PowerCLI already have commandlet to will enable/disable DRS host rules. But when you are in operations, you don’t really get to use the latest version of PowerCLI all the time. Anyway, this is just a placeholder for myself, but if you find it useful great for you.

Disclaimer: This is not my original work, I forgot where I ripped the codes from, but its not my original work. Apologize for the bad formatting.

#Filename = Set-DRSVMHostRules.ps1

$cluster = Get-Cluster 
$ClusName $rule = $cluster | 
  Get-DrsRule -Type VMHostAffinity -Name $RuleName 
$spec = New-Object VMware.Vim.ClusterConfigSpec
$ruleSpec = New-Object VMware.Vim.ClusterRuleSpec 
$ruleSpec.Info = $rule.ExtensionData 
$ruleSpec.Info.Enabled = $Enable 
$ruleSpec.Operation = "edit" 
$spec.rulesSpec += $ruleSpec 

$cluster.ExtensionData.ReconfigureCluster($spec,$true) | Out-Null
$cluster | Get-DrsRule -Type VMHostAffinity -Name $RuleName

get-cluster ClusterABCDC | Get-DrsRule -Type VMHostAffinity | 
% { .\Set-DRSVMHostRules.ps1 -ClusName $_.cluster -RuleName $ -Enable $true}
Leave a comment

Posted by on July 21, 2019 in powershell, vmware


Tags: , ,

vSphere: A colorful DCUI welcome screen

I was tasked to make our ESXi hosts compliant to the security standards and one of them is to set the legal message on DCUI. The work is easy enough, however you end up with only a black screen with the legal text and it looks very ugly!

So I chanced upon two links which shows you how you can add colors and format to the welcome message. Unfortunately, none of them shows you how to get it right.

Read the rest of this entry »
Leave a comment

Posted by on March 15, 2019 in powershell, Scripts, vmware


Tags: ,

Using REST API calls on VCE Vision

In VCE Vision, you can use REST API calls to grab the status of the overall system, drill down to the individual components and, when required e.g. for support calls, grab the full vblock information into an XML.

Conventionally, you would use a browser using the URL https://vision_core:8443/fm/systems to login and get the overall system’s health in an XML format. To grab the entire XML data you call https://vision_core:8443/fm/{dbid}/allblockdata. Over time, I find that grabbing allblockdata or drilling down to individual components can provide some insight to why a component is showing RED (for example, due connectivity error). Instead of logging in all the time, why not make REST API calls and grab the data instead?

Read the rest of this entry »
Leave a comment

Posted by on January 5, 2019 in powershell, Scripts


PS: How to get ESXi multipath storage with Powerpath

If your vSphere’s ESXi hosts are connected to storage via EMC’s powerpath, the convention methods of querying path info will not work. Basically, you will get zero results.

For example when you query the lun via MultipathInfo you may only see iSCSI luns listed (if you have them running), but not your datastores. Read the rest of this entry »

Leave a comment

Posted by on August 21, 2018 in powershell, vmware


PS: Getting a virtual machine’s properties in vRA

Update: Calling IaaS Web is no longer require as of 7.5 (I was using 7.0 previously), you can get the custom properties by making direct call to VRA. Furthermore, to call IaaS Web you need an account that has access to the Windows server, which may not be the same account to access VRA, so it becomes more painful.

Getting the properties of a VM provisioned in vRA is not straight forward, you need to get those information from the IaaS Web server instead of from vRA

This site shows you how you can get to it.

I am not sure which version the author has, but in version 7.2 the properties are not so straight forward and they strangely don’t expand when exported to a JSON file. However let’s first deal with the NTLM authentication on IaaS Web via a Invoke-RestMethod call. Its actually very straight forward, but hard to find; simply speaking, you use get-credential to capture the windows credentials and input it with -credential parameter.

Read the rest of this entry »
Leave a comment

Posted by on August 2, 2018 in powershell, vmware


Tags: ,

PS: Adding nodes to JSON object

Whilst working on vRA REST API to provision new virtual machines, one of task was to get the template for that catalog item and fill in required values.

One of the values common to most templates is the datacenter_location value. By default it will be null. That is not a big deal if you don’t care which datacenter your VM is being provisioned to, but what if you want to specify a location?

"_cluster": 1,
"_hasChildren": false,
"cpu": 1,
"datacenter_location":  null,

In this case the values should be as such

 "_cluster": 1,
"_hasChildren": false,
"cpu": 1,
"datacenter_location": {
               "classId": "Infrastructure.Compute.DataCenterLocations",
               "id": "DC1"

This is how you can add the values into the JSON object (I am assuming that you already gotten the bearer token in the $vratoken variable) Read the rest of this entry »

Leave a comment

Posted by on August 2, 2018 in powershell, vmware


Tags: , ,