1
00:00:01,240 --> 00:00:03,750
Now the whole point of working with variables is to

2
00:00:03,750 --> 00:00:06,540
make it easier to use PowerShell.

3
00:00:06,540 --> 00:00:07,210
For example,

4
00:00:07,210 --> 00:00:10,700
I'm going to create a variable called $logs and it's going to

5
00:00:10,700 --> 00:00:14,740
contain the results of the Get‑Winevent command and that's going

6
00:00:14,740 --> 00:00:17,740
to get the 1000 most recent events.

7
00:00:17,740 --> 00:00:21,940
I'm giving you some command examples that might take a long time to run,

8
00:00:21,940 --> 00:00:25,140
although in this case, it's really not going to take that long at all.

9
00:00:25,140 --> 00:00:30,140
But now I have a variable that holds the results of that command.

10
00:00:30,140 --> 00:00:34,600
I can now work with those objects just as if I had run Get‑Winevent so

11
00:00:34,600 --> 00:00:39,540
I can say take $logs and pipe it to Group‑Object.

12
00:00:39,540 --> 00:00:43,450
And so, I want to group on the LevelDisplay Name with NoElement.

13
00:00:43,450 --> 00:00:44,690
So I just want to see hey,

14
00:00:44,690 --> 00:00:48,340
what types of entries have I gotten in the system log for the

15
00:00:48,340 --> 00:00:53,540
last 1000 record or maybe I want to take all those logs and

16
00:00:53,540 --> 00:00:56,740
group them on the provider name.

17
00:00:56,740 --> 00:01:01,050
Same idea. What sources of problems or information

18
00:01:01,050 --> 00:01:02,950
am I getting in the event log?

19
00:01:02,950 --> 00:01:05,290
I don't have to rerun Get‑WinEvent.

20
00:01:05,290 --> 00:01:10,240
I can use the results that are saved to that variable.

21
00:01:10,240 --> 00:01:12,680
Now here is one that's a little more complicated.

22
00:01:12,680 --> 00:01:18,020
So let's take $logs and I'm going to pipe this to where object where the

23
00:01:18,020 --> 00:01:24,520
provider name is equal to service control manager, and I want to select the

24
00:01:24,520 --> 00:01:28,640
TimeCreated LevelDisplayName and the Message.

25
00:01:28,640 --> 00:01:29,120
So there we go.

26
00:01:29,120 --> 00:01:31,440
So again, I don't have to rerun Get‑Winevent.

27
00:01:31,440 --> 00:01:36,030
I can use the saved results really quite handy. Now

28
00:01:36,030 --> 00:01:40,240
sometimes the variable might change.

29
00:01:40,240 --> 00:01:41,190
So let me show you what I'm talking about.

30
00:01:41,190 --> 00:01:45,540
Let's go back and rerun that veg command to Get‑Vegetable,

31
00:01:45,540 --> 00:01:50,140
everything vegetables starts with c, save it to $veg.

32
00:01:50,140 --> 00:01:53,990
Alright, so there we can see. Now look at the corn object there,

33
00:01:53,990 --> 00:01:55,340
see there are 12.

34
00:01:55,340 --> 00:02:00,180
I'm going to use Set‑Vegetable and change the corn value to 1 and

35
00:02:00,180 --> 00:02:04,740
I'm using ‑Passthru so it'll update the results.

36
00:02:04,740 --> 00:02:10,220
Go back and look at the variable now, that changed the value.

37
00:02:10,220 --> 00:02:12,460
This doesn't happen all the time.

38
00:02:12,460 --> 00:02:15,650
Sometimes it does, sometimes it doesn't, there is no way for you

39
00:02:15,650 --> 00:02:20,200
to know unless you try it and see for yourself.

40
00:02:20,200 --> 00:02:23,680
It doesn't always, but it might. For example, here, look at this.

41
00:02:23,680 --> 00:02:29,840
Get‑Service BITS, we'll save that to variable $bits, and if I select the

42
00:02:29,840 --> 00:02:34,940
name and StartType, you can see it is set to Manual.

43
00:02:34,940 --> 00:02:42,400
If I change the StartType to Disabled and then select it

44
00:02:42,400 --> 00:02:46,450
again because I'm going to do ‑PassThru, so that changed the

45
00:02:46,450 --> 00:02:48,860
actual service and it disabled it.

46
00:02:48,860 --> 00:02:56,540
However my bits object, the variable, shows a StartType of Manual.

47
00:02:56,540 --> 00:02:59,890
So in this case, there is no connection between the

48
00:02:59,890 --> 00:03:03,640
variable and the source object.

49
00:03:03,640 --> 00:03:07,340
The next thing to take into account with variables is type.

50
00:03:07,340 --> 00:03:10,940
So let's go back and recreate $a equals to 2.

51
00:03:10,940 --> 00:03:16,140
And as I have shown you, $a, if I pipe this to Get‑Member, is an integer.

52
00:03:16,140 --> 00:03:18,470
The variable itself really is meaningless as what's

53
00:03:18,470 --> 00:03:23,140
inside the variable that count.

54
00:03:23,140 --> 00:03:28,240
I can do things like $a + $a and I get 4.

55
00:03:28,240 --> 00:03:31,990
So let's assign a variable $i. Now notice here I'm putting this

56
00:03:31,990 --> 00:03:35,140
in single quotes, which makes it a string.

57
00:03:35,140 --> 00:03:40,240
You can even see the color coding change that PowerShell did.

58
00:03:40,240 --> 00:03:42,140
Now let's try to add this because I think oh yeah,

59
00:03:42,140 --> 00:03:44,650
it's going to be, it didn't work.

60
00:03:44,650 --> 00:03:50,500
Nope. I was expecting 10, but I got 55 because PowerShell saw that I was

61
00:03:50,500 --> 00:03:55,350
treating that as a literal string 5, and so it just gave me 2 literal

62
00:03:55,350 --> 00:04:02,240
strings put together. And you can verify this if I pipe $i to Get‑Member

63
00:04:02,240 --> 00:04:06,990
and you can see that it is a string. Get‑Member will be a cmdlet that you

64
00:04:06,990 --> 00:04:10,140
will use all the time.

65
00:04:10,140 --> 00:04:16,320
Now what you could do is I can cast or tell PowerShell hey create $i,

66
00:04:16,320 --> 00:04:20,940
but make it an int32 and give it a value of 10.

67
00:04:20,940 --> 00:04:25,040
Normally, PowerShell if I just $i equals 10, it would do that automatically.

68
00:04:25,040 --> 00:04:26,530
You don't always have to do that.

69
00:04:26,530 --> 00:04:30,500
Sometimes it makes more sense to you if you see it go oh

70
00:04:30,500 --> 00:04:33,940
yeah that reminds me that's an int32.

71
00:04:33,940 --> 00:04:40,840
Now if I multiply that variable by 2, I get the value that I am expecting.

72
00:04:40,840 --> 00:04:46,410
Where it gets tricky or sometimes helpful is with dates. So I can take a

73
00:04:46,410 --> 00:04:55,340
string 12/31/2022 and turn that into a datetime object saved to variable $d

74
00:04:55,340 --> 00:05:01,540
and you see PowerShell treats it as a datem and if I pipe to Get‑Member,

75
00:05:01,540 --> 00:05:06,490
sure enough you can see that it is a system.datetime object. Now this is

76
00:05:06,490 --> 00:05:11,990
where it gets tricky and where you have to be careful. So I have $d,

77
00:05:11,990 --> 00:05:15,820
remember I just created it as a date. Now I'm trying to assign it a value

78
00:05:15,820 --> 00:05:20,830
of the string foo, and PowerShell will complain because it says,

79
00:05:20,830 --> 00:05:21,440
hey,

80
00:05:21,440 --> 00:05:27,350
$d is a datetime object and you're trying to give me a string and I can't

81
00:05:27,350 --> 00:05:30,300
make foo into a date so I'm going to give you an error.

82
00:05:30,300 --> 00:05:33,680
This is where you need to be careful with your variable names so that

83
00:05:33,680 --> 00:05:37,350
you make them meaningful and you're not reusing variables, although I do

84
00:05:37,350 --> 00:05:40,040
all the time and you probably will too, although just be aware that you

85
00:05:40,040 --> 00:05:43,630
may encounter this problem if you created a variable and forgot what

86
00:05:43,630 --> 00:05:45,840
type that you cast it to.

87
00:05:45,840 --> 00:05:48,970
So there are ways around that. I can say hey take $d and

88
00:05:48,970 --> 00:05:55,840
create a string of foo and now that will work.

89
00:05:55,840 --> 00:05:57,440
So you really need to be careful with the variable

90
00:05:57,440 --> 00:06:00,540
names and how you are using them.

91
00:06:00,540 --> 00:06:04,440
Let me show you one more slightly advanced alternative.

92
00:06:04,440 --> 00:06:08,840
So I'm going to take $t and assign a string to it.

93
00:06:08,840 --> 00:06:13,120
And now you can see $t, it looks like a date, but really if I pipe to

94
00:06:13,120 --> 00:06:19,200
Get‑Member, you can see, sure enough, it is a string, but I can tell

95
00:06:19,200 --> 00:06:23,940
PowerShell, hey I want to treat that as a datetime object.

96
00:06:23,940 --> 00:06:30,640
Take $t and treat it as a daytime object and PowerShell certainly does.

97
00:06:30,640 --> 00:06:32,860
This doesn't change the value of t.

98
00:06:32,860 --> 00:06:35,450
T Is still a string which has a length property.

99
00:06:35,450 --> 00:06:40,140
So all I did was a one‑time operation that just said take $t,

100
00:06:40,140 --> 00:06:42,570
turn it into a date, write it to the pipeline.

101
00:06:42,570 --> 00:06:46,440
It leaves t alone, it doesn't change that at all.

102
00:06:46,440 --> 00:06:46,940
That's again,

103
00:06:46,940 --> 00:06:48,660
something we'll cover a little bit later in the course,

104
00:06:48,660 --> 00:06:50,380
but I wanted to give you a taste.

105
00:06:50,380 --> 00:06:55,210
Alright, so that's a look at using variables in PowerShell and why you might

106
00:06:55,210 --> 00:07:00,000
want to do them, some of the things you might run into. Let's go back and look at some more slide.

