Scripting Corner: Command line arguments and Changing VM’s configuration parameters with PowerCLI

Sometimes it is useful to run a PowerShell script with commandline arguments in order to make the action faster – be it automation of the workflow or a number of batch jobs where foreach wouldn’t be usable.

The arguments you feed into your powershell scripts are stored in the global $args array, and we will operate with them as such.

Let’s assume we want to make a powershell script that will allow our 2nd level colleagues to be able to change the Virtual Machine’s Advanced Parameters without going to vCenter, having to shut down the VM, change values and then power it back on again – this would be done in a vCenter Orchestrator workflow that actually calls this script with parameters as inputs.

If you would do

.\test.ps1 a b c

The $args variable would be an array of @("a","b","c") – now you would like to pass these arguments further to your function either by putting them in its definition, or its code using the param(parameters). You can put a data type before the argument’s variable into brackets, such as string, int etc.

# Define the parameters while initializing the function and pass them to variables used inside
function main([string]$myparam = $args[0], [string]$yourparam = $args[1], [string]$anotherparam = $args[2]) {
Write-Host $myparam $yourparam $anotherparam
}

#Or you can do it like this, with param(parameters) inside the function itself:
function main2() {
#the param must be the very first line after the function definiton starts!
param([string]$myparam = $args[0], [string]$yourparam = $args[1], [string]$anotherparam = $args[2])
Write-Host $myparam $yourparam $anotherparam
}

Everytime you invoke either “main a b c” or “main2 a b c” you will have the output of “a b c” written in the console.

If this is a .ps1 file, you will just add the name of the function to the last line of your script. This will execute the function using the command-line arguments as its variables.

Pretty straightforward, right? You can find the whole script to change an advanced VM parameter below. No error checking with Try, Catch included yet (this will be a topic for next scripting corner), as this script expects an automated input.

# USAGE: .\script.ps1 vcenter virtualmachine advancedparametername advancedparametervalue
# EXAMPLE: .\script.ps1 myvcenter.org TESTVM01 disk.enableUUID false

# Define a function, get variables from the command line feed
function main([string]$vic = $args[0], [string]$vm = $args[1], [string]$parameter = $args[2], [string]$paramvalue = $args[3]) {

# Connect to vCenter
Write-Host "Connecting to vCenter" $vic
Connect-VIServer $vic | Out-Null

# Get VM...
Write-Host "Getting VM..." $vm
$target = Get-VM $vm

# ...and fetch its advanced settings...
Write-Host "Fetching advanced settings of the VM..."
$targetpara = $target | Get-AdvancedSetting | ? {$_.Name -like "$parameter"}

# Compare the new and current settings and proceed accordingly
If ($targetpara.value -ne $paramvalue) {
Write-Host "Applying newly configured settings..." `n "Name: " $parameter `n "Value: " $paramvalue

Set-AdvancedSetting -AdvancedSetting $targetpara -Value "$paramvalue" -Confirm:$false | Out-Null

Write-Host "Done!" -foregroundcolor Green
}
Else {
Write-Host "The target value has alredy been set!"
}

# Finally disconnect from the vCenter.
Disconnect-Viserver $vic | Out-Null
}

# initiate the function
main

See you around in the next Scripting Corner 😉

Advertisements

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s