RSS

Tag Archives: powershell

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

Powershell: Batch removing datastores from ESXi hosts (4.1)

We have to repurpose a bunch of 10 ESXi host for another purpose and I need to remove all the 12 datastores already mounted on the hosts. Here is a one-liner which I used to remove datastores quickly using vsphere powershell CLI commands. Note hosts.txt and ds.txt contains a list of hosts and datastores which you want to remove. Broke down into paragraphs for readability, this should be a one-liner. This is for 4.1 hosts. 5.0 host and beyond may require a different command due to storage I/O controls.

get-content c:\scripts\hosts.txt |

% { $h = $_ ; get-content c:\scripts\ds.txt |

%{ "Host="+$h+" : Removing Datastore... "+$_ ;

remove-datastore -Datastore $_ -VMhost $h -confirm:$false}}

 
Leave a comment

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

 

Tags: , ,

Grabbing NTP server list from VMHosts in Vcenter

Due to some changes to our NTP server, I needed to review all the ESX hosts the region. This page nicely summarises all the cmdlet we can and should use:

http://www.amikkelsen.com/?p=458

Here is the one-liner which I used to grab NTP values off ESX servers (note: below is for readability, they should be in a single line). This will output a tab delimited file with ESX host name and a comma-delimited NTP server list.

$vc = “vcenter-01” ;

connect-viserver $vc -user VC_admin –password ##### ;

get-vmhost | % {$_.name + “`t” + ((get-vmhostntpserver $_.name) -join “,”)

| out-file “c:\temp\$vc-ntp.txt” -append} ;

disconnect-viserver $vc -confirm:$false

 
Leave a comment

Posted by on April 5, 2013 in powershell, Scripts, vmware

 

Tags: , ,

Powershell: Mulitple match in the same string using regex

I have a variable $a = “Kelvin (kel1), Mary (mar2), Tim (tim4)”, which I was to use match to extract all the text within the brackets.

If you use $a -match “\(\w*\)”, you will end up with only the first match (that is “(kel1)”, but I am wanted 3 matches within the brackets to be returned.

After digging into this site: http://www.powershelladmin.com/wiki/Powershell_regular_expressions, I found what was looking for. The below expression should extract multiple matches within a single string.

[regex]::Matches($a,’\(\w*\)’,’MultiLine’) | % {$_.Value}

 
Leave a comment

Posted by on February 19, 2013 in powershell, Scripts

 

Tags: