Once again Don nails a
chapter title. Today we are learning about variables and the different ways of
working with them. In my past with PowerShell and working with other peoples
scripts I can see that variables can and are used as a primary means of storing
not only objects but also commonly used commands within a script.
To create a variable in PowerShell
you need only declare a variable name preceded by a dollar sign ($) and then
follow it with an equal sign (=) and what it contains. Lets give it a try.
$computername =
'server1'
You just created a variable
with server1 as its object. You can store multiple objects or collections into
a variable as well.
$computername
= 'server1','server2','server3','localhost'
Now to recall the objects
within the variable simply type the variables name.
$computername
In your console you will see
a list of all the objects stored within the variable. With the preceding
example there is also something else that we learned in this chapter. Notice the
single quotes around each name. This denotes to PowerShell that what is
contained inside those single quotes is a literal string. This was a little
confusing to me so I went over the examples in the book several times to make
sure that I had it. Here is the example:
$var =
'What does $var contain'
$var
The output you will see on
this is the string "What does $var contain". The second part of this
example is what makes this make sense.
$computername
= 'server-r2'
$phrase
= "The computer name is $computername"
$phrase
The output here will show
"The computer name is server-r2". Give it a try. If we were to have
enclosed the $phrase text string in single quotes the output would have been
exactly as we had typed them, The computer name is $computername. Something to remember
here. If you use a variable inside of another variable, as soon as you hit
enter to store it, it parses it and stores that as the variable. If you happen
to change the original variable, in this case $computername, the output of the $phrase
would remain the same, The computer name is server-r2.
The second thing I found very
useful in this chapter was the use of the backtick character. The backtick is
an escape character in PowerShell. Basically it removes the Special meaning of
other characters or adds special meaning to the characters following it. Here
is the book example:
$computername = 'Server-R2'
$phrase = "`$computername contains $computername
$phrase
The output of this variable
will be "$computername contains Server-R2". Notice the first instance
of the variable was not processed but the second was. That is because the backtick
removes the power of the dollar sign to parse a variable.
Ok so you can store one or
more objects in a variable, how is this useful. With the examples from the book
we were simply using string values but in the lab work we take that a step
farther. We are asked to pull information from the win32_Bios class for two
computers stored as a variable and run it as a background job, receive that job
information into a variable, View the variable, and then export that as a
CLIXML document. Here is how I did it using variables and some of the other
stuff I have learned along the way.
$computer = 'mycomputername','localhost' (I only have
one computer for this)
$job = get-wmiobject win32_bios -computername
$computer -asjob
$biosinfo = Receive-Job -job $job
$biosinfo
$biosinfo | export-CLIXml -path
c:\powershell\Bios_Info.xml
import-clixml -path c:\powershell\bios_info.xml
Just to verify I imported the
info to ensure it was stored properly. Variables can be pretty powerful stuff
in PowerShell as you can see. There is so much more to learn about variables
but I just don’t have the space today. I believe I will revisit this in another
post. Until tomorrow, happy powershelling