Scripting with PowerShell

It has been a few weeks since the last blogpost on PS now, so it is indeed time for a new one. I'm getting hang of this PS scripting environment and I think it's time again to share some knowledge. This time around I will dig more into how to create scripts and especially how we pass data to the script in different ways.


Scripts are useful because they are both simple and complicated at the same time. Simple in the sense that they are easy to create, and simple to use. Complicated because they can do advanced sets of tasks so that you don't have to do them over and over again. One task that I need to do is to set a working environment for programming in Windows. I need to configure paths and other environment variables. I also need to create simple projects and want them to be organized in a special way. I want to do this simple and quickly without passing a whole heap of lines to my shell. This means that I need to build myself a tool that can execute commands, copy and organize files the way I want them and get the information it needs to do this.

A scenario where the script takes input.

There are many scenarios for giving input to a script. I need to setup my environment to build Qt applications. I have several libraries and I want to be able to choose which one I want to build against every time. I also want my script to know when I don't need to set the environment when it is already set. I can do this in two ways: First by passing the information as an argument when I call the script. Second, by letting the script ask me for the info it needs.

Passing an argument

To pass the information as an argument I just add a key word to the script name when I call it. This keyword is of my own choice. Here I am passing the directory name of the Qt library I want to use.

PS C:\mytools> ./qtv 4.5Desktop

The script is named qtv.ps1 thus I type ./qtv 4.5Desktop. I can use ./ since I am inside the same directory as the script. Had I not, I would have needed to complete the path to the script. In addition I typed 4.5Desktop which is the name of the Qt library directory I want to use. Now let's see how the script should use this info.

# All arguments passed to the script are organized in an array.
# This means that we can navigate this array and use the
# arguments based on their position in the call. To navigate we
# follow the normal index numbering.

$firstArg = $args[0]
$firstArg

$secArg = $args[1]
$secArg

This would give us the following output:

PS C:\mytools> 4.5Desktop

You would only get the first argument printed since there was no second one and the variable would be NULL.

Prompt the user for data

The other way for the script to get your input is to simply ask you while the process is running. This might be the case when the script cannot understand the input you passed or if the processes are dependent on making choices based on changing conditions while running.

# We assign the value from Read-Host to $qtDirectory
$qtDirectory = Read-host "Please enter the complete path to your Qt library"

# And for a simple check-up we output is in the shell
$qtDirectory

That would give us an output like this.

PS C:\mytools> ./qtv
Please enter the complete path to your Qt library: 4.5Desktop
4.5Desktop
PS C:\mytools>

I'll cut this entry in two parts. The next part I will use the information to set the environment. As usual I would love to have your comments and questions. So until next time - happy scripting.

Comments

Popular posts from this blog

Designing and programming - Part 2

Filtering Dropdown choices in a Power Pages form using Dataverse Relations

Exploring the Power of Variables in Liquid and Power Pages