1
00:00:02,140 --> 00:00:04,790
Let's jump back into PowerShell and let me give you a quick

2
00:00:04,790 --> 00:00:08,220
demonstration of how this variable expansion process works or,

3
00:00:08,220 --> 00:00:10,540
in some cases, doesn't work.

4
00:00:10,540 --> 00:00:14,620
Okay, variable expansion in PowerShell can be a tricky thing,

5
00:00:14,620 --> 00:00:16,810
especially for a lot of PowerShell beginners.

6
00:00:16,810 --> 00:00:21,320
I see this all the time in classes that I teach and speaking at conferences.

7
00:00:21,320 --> 00:00:21,660
So,

8
00:00:21,660 --> 00:00:23,710
let me go through a few demonstrations here so you can

9
00:00:23,710 --> 00:00:26,940
see how this works and what to expect.

10
00:00:26,940 --> 00:00:31,540
So, let's say I've got this variable $PSEdition, and you can see it says Core.

11
00:00:31,540 --> 00:00:39,920
So I want to create a string using double quotes,

12
00:00:39,920 --> 00:00:43,290
and it says I'm running the, and then $PSEdition,

13
00:00:43,290 --> 00:00:45,740
the variable name, of PowerShell,

14
00:00:45,740 --> 00:00:49,530
and PowerShell writes a string and replaces $PSEdition,

15
00:00:49,530 --> 00:00:54,090
that variable name, with the value of the variable.

16
00:00:54,090 --> 00:00:55,010
Again,

17
00:00:55,010 --> 00:00:57,620
this is something that you probably will use more when you get to scripting,

18
00:00:57,620 --> 00:01:00,320
but I wanted to show it to you in the console because you

19
00:01:00,320 --> 00:01:02,920
can also use this up from the console.

20
00:01:02,920 --> 00:01:05,110
The variable expression in double quotes,

21
00:01:05,110 --> 00:01:12,060
that's the key here, works nicely with a simple variable like $PSEdition or $PID,

22
00:01:12,060 --> 00:01:14,740
anything has a a single value.

23
00:01:14,740 --> 00:01:16,640
This is where it gets complex.

24
00:01:16,640 --> 00:01:20,640
So let's get the WinRM service, save that to a variable.

25
00:01:20,640 --> 00:01:22,690
And we can look at a few properties here.

26
00:01:22,690 --> 00:01:24,840
So I've got name and status.

27
00:01:24,840 --> 00:01:27,900
I want to create a string, a similar string.

28
00:01:27,900 --> 00:01:31,610
This is basically going to say the WinRM service is running,

29
00:01:31,610 --> 00:01:34,040
at least that's what I'm expecting.

30
00:01:34,040 --> 00:01:35,330
A lot of people will think, well,

31
00:01:35,330 --> 00:01:37,130
I'll just do the same thing that I just did with

32
00:01:37,130 --> 00:01:39,520
the with that $PSEdition example.

33
00:01:39,520 --> 00:01:45,420
The $svc.name is currently $svc.status, it's using that object notation.

34
00:01:45,420 --> 00:01:47,120
And that fails.

35
00:01:47,120 --> 00:01:52,130
You instead get the System.ServiceProcess.ServiceController.name,

36
00:01:52,130 --> 00:01:52,820
blah, blah, blah.

37
00:01:52,820 --> 00:01:55,040
Yeah, what happened?

38
00:01:55,040 --> 00:01:58,240
What happened is that PowerShell saw $service,

39
00:01:58,240 --> 00:02:01,620
$svc, and said okay, I see that that is an object.

40
00:02:01,620 --> 00:02:02,900
Now I know you want the name property,

41
00:02:02,900 --> 00:02:09,740
but I don't know how to expand the property to give you just that single value.

42
00:02:09,740 --> 00:02:14,160
So I'm just going to kind of do my best effort here, and so I'm going to

43
00:02:14,160 --> 00:02:17,960
show you the object and the property that you wanted, actually the name of

44
00:02:17,960 --> 00:02:21,340
the property. So you can see its name and status.

45
00:02:21,340 --> 00:02:25,640
What you need to do is use a subexpression.

46
00:02:25,640 --> 00:02:27,590
Now it's a little tricky, but if you think about it

47
00:02:27,590 --> 00:02:30,540
in a moment it will make sense.

48
00:02:30,540 --> 00:02:35,540
So let me type it out here and then explain what's going on.

49
00:02:35,540 --> 00:02:40,260
So, I have The, and then the next part is the variable part,

50
00:02:40,260 --> 00:02:41,840
the fill in the blank.

51
00:02:41,840 --> 00:02:43,370
I still have $service,

52
00:02:43,370 --> 00:02:49,710
$svc.name, but then in parentheses I have a dollar sign in front of that,

53
00:02:49,710 --> 00:02:57,520
and the same thing for $svc.status. PowerShell will take that dollar sign,

54
00:02:57,520 --> 00:03:02,340
parentheses, and then the code inside it and run it or evaluate it and in

55
00:03:02,340 --> 00:03:10,360
essence say oh, $svc.name, that is WinRM, $svc.status, that is running. And

56
00:03:10,360 --> 00:03:17,690
it will then take that in‑memory value, that's kind of an ad hoc temporary

57
00:03:17,690 --> 00:03:22,610
variable, and then plug it into the placeholder that you have there in the

58
00:03:22,610 --> 00:03:24,040
string.

59
00:03:24,040 --> 00:03:27,240
And now I get the results that I am expecting.

60
00:03:27,240 --> 00:03:31,140
So if you have a complex string that you're trying to create with

61
00:03:31,140 --> 00:03:36,400
objects and whatnot, subexpression, that is the key to making that

62
00:03:36,400 --> 00:03:40,040
work. Again, make sure you use double quotes.

63
00:03:40,040 --> 00:03:41,530
If you use single quotes,

64
00:03:41,530 --> 00:03:45,050
PowerShell assumes that anything you put inside those single

65
00:03:45,050 --> 00:03:48,800
quotes is treated as a literal string and it will not try to

66
00:03:48,800 --> 00:03:53,140
replace it or expand it or anything like that.

67
00:03:53,140 --> 00:03:53,440
Now,

68
00:03:53,440 --> 00:03:57,210
one thing I want to point out is learn to use the

69
00:03:57,210 --> 00:03:59,240
subexpressions as I've shown you here.

70
00:03:59,240 --> 00:04:02,340
What I really don't want to see is if you show me code

71
00:04:02,340 --> 00:04:11,760
that does this. Now it will work, but it's really, to my

72
00:04:11,760 --> 00:04:13,970
way of thinking, an old school,

73
00:04:13,970 --> 00:04:18,520
almost a VBScript approach to PowerShell. I'm concatenating

74
00:04:18,520 --> 00:04:20,970
much of the strings together to get the result that I want.

75
00:04:20,970 --> 00:04:22,910
When I see code like this,

76
00:04:22,910 --> 00:04:27,310
I know that whoever is writing this is still thinking about text, they're

77
00:04:27,310 --> 00:04:30,420
thinking about how do I join pieces of text together?

78
00:04:30,420 --> 00:04:33,840
They're not thinking about PowerShell as an object.

79
00:04:33,840 --> 00:04:36,220
The subexpression forces you to think, okay,

80
00:04:36,220 --> 00:04:40,090
$svc is an object, how do I work with that object?

81
00:04:40,090 --> 00:04:43,730
Don't be thinking about how do I pull out a piece of string,

82
00:04:43,730 --> 00:04:47,550
a piece of text that I can work with, because that will lead you down the path

83
00:04:47,550 --> 00:04:51,300
of concatenation. Personally, the plus sign is for addition,

84
00:04:51,300 --> 00:04:55,440
for adding numbers, not for adding strings together.

85
00:04:55,440 --> 00:05:04,000
All right, enough of my little sermon on concatenation with strings. Let's hop back to the slides and finish up this module.

