RSS

Tag Archives: powershell

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 »

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

PS:One liner to grab IP from DNS name

Needless to say, it’s not hard to grab IP address from a DNS name. There are at least 2 commands that come to mind:

[System.Net.Dns]::GetHostAddresses($hostname)
Test-Connection $hostname

But what if you want to grab the IP address of a list of host names AND ensure that the command doesn’t skip the output if the host name is unresolved? Read the rest of this entry »

 
Leave a comment

Posted by on January 23, 2018 in powershell

 

Tags: ,

Powershell and splatting: passing switch parameter to another script

I came across the term splatting previously, but never really paid heed to it until recently when I needed to pass a switch parameter as a variable to another script, like this:

param(
[string]$Name,
[string]$Server,
[switch]$SkipHost
)
if ($SkipHost) {$s = "-SkipHost"} else {$SkipHost = ""}
.\script1.ps1 -Name $name $s
.\script2.ps1 -Name $name -Server $server

The line at script1.ps1 obviously don’t work and will cause an error when executed.

Some folks may ask: “why do I need to do this?” Well, splatting is most useful when you have a main script which calls other external scripts. This is where splatting comes in, its the ability to pass parameters into a script using variables. I don’t need to explain the mechanics about it as there are many posts elsewhere devoted to this already. Suffice to say, despite all the fancy ways in which various authors had devoted their time to describe the use of @PSBoundParameter, they are only useful if you are passing ALL the parameters at one go. If one need to pass only one or two of the parameters, @PSBoundParameter probably makes the script more complex.

One way to make this work without @PSBoundParameter is via the Powershell version 1 method:

param(
[string]$Name,
[string]$Server,
[switch]$SkipHost
)
.\script1.ps1 -Name $name -SkipHost:$SkipHost
.\script2.ps1 -Name $name -Server $server
 
Leave a comment

Posted by on September 21, 2015 in powershell, Scripts

 

Tags:

PS1: Mashing UCS host to MAC address via vlookup

This post is more a note to self, but is useful for folks using Cisco UCS enclosure when you need to general a list of hosts against its MAC address on eth0 NIC.

Cisco UCS is notoriously flexible yet complex. If you want a list of physical host names against its MAC address, there is no direct means of exporting that list. You need to get the source from two exports, one from the server export and one from the MAC Identity Management tab in the LAN tab. Then you need perform a vlookup to match each host to its MAC address using “Assigned To” field in th MAC export and “Name” field in the server export. Read the rest of this entry »

 
Leave a comment

Posted by on September 21, 2015 in powershell, Scripts

 

Tags: ,

PowerShell: Setting JVM heap sizes for vCenter server 5.x

One of the changes from vCenter 5.0 to 5.1 that affect java memory allocation is vFabric tomcat server. This server effective decouples the java heap size memory management to the various vCenter server components, when this used to be a single setting in the tomcat configuration. An effect of this is that you many need to adjust the JVM heap size of your vCenter after upgrading (or even fresh install), even though you have specific the correct sizing during install.

From my experience, since we had upgraded vCenter from 5.0 to 5.5, the JVM heap size may not be sized correctly after the upgrade. Especially the Inventory servces, which can impact performance is set too low. Read the rest of this entry »

 
Leave a comment

Posted by on July 10, 2015 in powershell, Scripts, vmware

 

Tags: , ,

Powershell: Batch update of DNS server list in ESXi hosts

A quick one-liner to update the DNS serer list of all ESXi hosts in a cluster.

get-vmhost -location "MyVMClusterName |
get-vmhostnetwork |
set-vmhostnetwork -dnsaddress "1.1.1.1","2.2.2.2"

 
Leave a comment

Posted by on June 28, 2013 in powershell, Scripts, vmware

 

Tags: , , ,