Let's jump back into PowerShell and let me give you a quick
demonstration of how this variable expansion process works or,
in some cases, doesn't work.
Okay, variable expansion in PowerShell can be a tricky thing,
especially for a lot of PowerShell beginners.
I see this all the time in classes that I teach and speaking at conferences.
So,
let me go through a few demonstrations here so you can
see how this works and what to expect.
So, let's say I've got this variable $PSEdition, and you can see it says Core.
So I want to create a string using double quotes,
and it says I'm running the, and then $PSEdition,
the variable name, of PowerShell,
and PowerShell writes a string and replaces $PSEdition,
that variable name, with the value of the variable.
Again,
this is something that you probably will use more when you get to scripting,
but I wanted to show it to you in the console because you
can also use this up from the console.
The variable expression in double quotes,
that's the key here, works nicely with a simple variable like $PSEdition or $PID,
anything has a a single value.
This is where it gets complex.
So let's get the WinRM service, save that to a variable.
And we can look at a few properties here.
So I've got name and status.
I want to create a string, a similar string.
This is basically going to say the WinRM service is running,
at least that's what I'm expecting.
A lot of people will think, well,
I'll just do the same thing that I just did with
the with that $PSEdition example.
The $svc.name is currently $svc.status, it's using that object notation.
And that fails.
You instead get the System.ServiceProcess.ServiceController.name,
blah, blah, blah.
Yeah, what happened?
What happened is that PowerShell saw $service,
$svc, and said okay, I see that that is an object.
Now I know you want the name property,
but I don't know how to expand the property to give you just that single value.
So I'm just going to kind of do my best effort here, and so I'm going to
show you the object and the property that you wanted, actually the name of
the property. So you can see its name and status.
What you need to do is use a subexpression.
Now it's a little tricky, but if you think about it
in a moment it will make sense.
So let me type it out here and then explain what's going on.
So, I have The, and then the next part is the variable part,
the fill in the blank.
I still have $service,
$svc.name, but then in parentheses I have a dollar sign in front of that,
and the same thing for $svc.status. PowerShell will take that dollar sign,
parentheses, and then the code inside it and run it or evaluate it and in
essence say oh, $svc.name, that is WinRM, $svc.status, that is running. And
it will then take that in‑memory value, that's kind of an ad hoc temporary
variable, and then plug it into the placeholder that you have there in the
string.
And now I get the results that I am expecting.
So if you have a complex string that you're trying to create with
objects and whatnot, subexpression, that is the key to making that
work. Again, make sure you use double quotes.
If you use single quotes,
PowerShell assumes that anything you put inside those single
quotes is treated as a literal string and it will not try to
replace it or expand it or anything like that.
Now,
one thing I want to point out is learn to use the
subexpressions as I've shown you here.
What I really don't want to see is if you show me code
that does this. Now it will work, but it's really, to my
way of thinking, an old school,
almost a VBScript approach to PowerShell. I'm concatenating
much of the strings together to get the result that I want.
When I see code like this,
I know that whoever is writing this is still thinking about text, they're
thinking about how do I join pieces of text together?
They're not thinking about PowerShell as an object.
The subexpression forces you to think, okay,
$svc is an object, how do I work with that object?
Don't be thinking about how do I pull out a piece of string,
a piece of text that I can work with, because that will lead you down the path
of concatenation. Personally, the plus sign is for addition,
for adding numbers, not for adding strings together.
All right, enough of my little sermon on concatenation with strings. Let's hop back to the slides and finish up this module.