The humble variable

Powershell has mushroomed recently into almost every facet of systems administration. From Exchange servers to system information management, IT professionals without programming backgrounds are working with scripts more than ever.

The ability to automate almost every aspect of operations is tantalizing. But before we unleash an automated process on critical infrastructure, we should understand what it does.

To that end, let’s look at a fundamental idea: the variable.

Remember when we talked about arrays last month? A series of containers to store your stuff that could be referenced by name or item number?

The variable is even simpler. A variable is a single container that you create. All it has is a name. What is in the container could stay static, or it could change. Think of it like a bucket with a name written on it. The variable name stands for everything in the bucket.

Variables are commonly used in a couple of ways.

The first is to store a value temporarily. This is often present in browser-side scripting like Javascript. If the operations performed on the page don’t really require a big database or even an array, why not just create one bucket to hold that value?

In Javascript, PHP or Powershell, the variable comes into existence as soon as you declare it.

$mybucket = "The text string I'm storing in the variable called mybucket";

In one line, we’ve created a new variable called mybucket and put something inside of it: a string of text. We could just as easily fill it with an integer, a URL or a filename on our web server. From this moment forward, the entire text string can be represented simply by the variable name. Want to change the text in your form field?

document.getElementByID("mytextfield").value = $mybucket;

In Powershell and PHP, declaring a variable also starts with a $ sign.

$rcpath = "c:\users\jimmyjoe\appdata\local\microsoft\outlook\roamcache";

Want to write a script that works with Jimmy Joe in accounting’s Outlook cache for Autocomplete entries, but don’t feel like typing out the entire path every time you want to reference it? Your friend the variable has your back.

So when you’re staring down the unforgiving scramble of an administration script with a bunch of $ signs in it, don’t panic. They’re only buckets, and now you know a little bit more about what they do.

The Array – Your Closet Organizer

There comes a time when you have a lot of stuff and need to put it somewhere. But you also need to easily access each piece of stuff in a hurry. Enter the array, which is sort of like a shelf or an organizer for your closet. An array is a container with many compartments.

The array has a name and stores each item inside. Each item is referred to by a number, starting with 0. Say we wanted an array to hold the names of cars. In Javascript, it looks like this:

carnames = new Array("ferrari","bugatti","porsche","volkswagen");

In this case, the name of the array is carnames. The array contains four items. They’re referenced by numbers 0, 1, 2 and 3. The numbering of array items starts with zero.

When you want to use one of the items from your closet organizer, you can call it by the name of the array and its corresponding number, like this:

carnames[0]

Say you had a page element that you wanted to pull in the name ferrari. Your page element is called label. Your request could look something like this:

document.getElementById("label").value = carnames[0];

Arrays can be used to store text strings, numerical values and even file names. Say you had an image on your page that you wanted to swap out with others from a selection of images. Your array could look this way:

pictures = new Array("img/picture1.jpg","img/picture2.jpg","img/picture3.jpg");

Your element on the page is called imgdrop. A reference to swap the image for the file called picture2.jpg could look like this:

document.getElementById("imgdrop").src = pictures[1];

Arrays are a wonderful tool for many uses. You can read more about them in this handy tutorial!