RSS

Category Archives: vmware

PowerCLI: Backup VDswitch and VDportgroups

Here is a quick set of scripts to export and backup your VDswitches. It is pretty straight forwards. It dumps VDswitch exports with and without port groups and the VDportgroups into their own folders. Then zip up each folder and finally create an archive from that 3 folders into a single backup zip. You can delete the folders and the other zip files after it ran successfully

#first connect to your vcenter with linked mode if you have


New-Item -Name noportgroup_vdswitch -ItemType Directory
Get-VDSwitch | % {Export-VDSwitch -VDSwitch $_ -Destination ".\noportgroup_vdswitch\NoPort_$($_.datacenter)_$($_.name)_$($_.id).zip" -WithoutPortGroups }
Compress-Archive -Path .\noportgroup_vdswitch -DestinationPath .\noportgroup_vdswitch



New-Item -Name fullportgroup_vdswitch -ItemType Directory
Get-VDSwitch | % {Export-VDSwitch -VDSwitch $_ -Destination ".\fullportgroup_vdswitch\fullport_$($_.datacenter)_$($_.name)_$($_.id).zip" }
Compress-Archive -Path .\fullportgroup_vdswitch -DestinationPath .\fullportgroup_vdswitch

New-Item -Name vdportgroup -ItemType Directory
Get-VDPortgroup | % { Export-VDPortGroup -VDPortGroup $_ -Destination ".\vdportgroup\PG_$($_.datacenter)_$($_.vdswitch)_$($_.name)_$($_.id).zip" }
Compress-Archive -Path .\vdportgroup -DestinationPath .\vdportgroup


Compress-Archive -LiteralPath .\vdportgroup.zip,.\fullportgroup_vdswitch.zip,.\noportgroup_vdswitch.zip -DestinationPath .\vdswitch_backup_$($(date).tostring("yyyyMMdd"))
 
Leave a comment

Posted by on June 26, 2020 in powercli, vmware

 

Tags: ,

Vsphere_vchecklog

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 -> https://github.com/kwongheng/vsphere_vchecklog

 
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

 

VRA’s rest api filter quirkiness

Here is one of the quirky inconsistencies with using filters in VRA’s rest api.

These are the fields in a catalogitem using REST API call “catalog-service/api/catalogItems”

type : CatalogItem
callbacks :
catalogItemTypeRef : @{id=com.vmware.csp.component.cafe.composition.blueprint; label=Composite Blueprint}
dateCreated : 2017-02-03T19:31:51.793Z
description :
forms : @{itemDetails=; catalogRequestInfoHidden=True; requestFormScale=BIG; requestSubmission=; requestDetails=; requestPreApproval=; requestPostApproval=}
iconId : composition.blueprint.png
id : b5022c80-a00f-4b46-8124-2dffc6b98561
isNoteworthy : False
lastUpdatedDate :
name : Import RP4VM Shadow
organization : @{tenantRef=xxx; tenantLabel=xxx; subtenantRef=; subtenantLabel=}
outputResourceTypeRef : @{id=composition.resource.type.deployment; label=Deployment}
providerBinding : @{bindingId=cloud!::!ImportRp4vmShadow; providerRef=}
serviceRef :
status : PUBLISHED
statusName : Published
quota : 0
version : 0
requestable : True

You would think that to make a rest api called to filter catalogitems by catalogItemTypeRef of “Composite Blueprint”, I should make a call like this:

catalog-service/api/catalogItems?page=1&limit=100&%24filter=catalogItemTypeRef/label eq 'Composite Blueprint'

This is incorrect in two places, the correct syntax is

 catalog-service/api/catalogItems?page=1&limit=100&%24filter=catalogItemType/name eq 'Composite Blueprint'

1. Any fields with name of type xxxxxxTypeRef should be referenced without “Ref” when filtering, e.g. outputResourceTypeRef is outputResourceType and of course, catalogItemTypeRef is catalogItemType.

2. Attribute “label” should be “name” when using in the filter.

No where were these explained explicitly in VMware document and I had to look at a few examples in the VMware docs to figure it out.

 
Leave a comment

Posted by on January 12, 2020 in Cloud, 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
param(
[Parameter(Mandatory=$true)]
[String]$ClusName,
[Parameter(Mandatory=$true)]
[String]$RuleName,
[Parameter(Mandatory=$false)]
[Bool]$Enable=$true
)

$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


#Example
get-cluster ClusterABCDC | Get-DrsRule -Type VMHostAffinity | 
% { .\Set-DRSVMHostRules.ps1 -ClusName $_.cluster -RuleName $_.name -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: ,

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: , ,

PS+REST:NSX Central CLI using REST API

As a second part from my previous post, I modified the script to run Central CLI. One thing to note, you need to add “accept”=”text/plain” into the header when running central CLI command or you will get 406 errors (sorry, I found this out in one of the blogs, but couldn’t give the proper credits).

Lastly the request returns a bunch of text and not XML or JSON.

nsxoutput

Read the rest of this entry »

 
Leave a comment

Posted by on February 15, 2018 in powershell, vmware

 

Tags: , , ,