1
00:00:00,340 --> 00:00:04,340
So let's go into our environment, and we'll look at how we

2
00:00:04,340 --> 00:00:08,210
create a single computer remote session, and then we'll look at

3
00:00:08,210 --> 00:00:11,670
how we create multiple computer remote sessions by connecting to

4
00:00:11,670 --> 00:00:15,240
those extra member servers.

5
00:00:15,240 --> 00:00:18,220
Now, creating sessions is really simple.

6
00:00:18,220 --> 00:00:22,180
Now if we're connecting to a single computer, then of course, we do

7
00:00:22,180 --> 00:00:27,840
have the ability to use what's called Enter‑PSSession.

8
00:00:27,840 --> 00:00:31,730
I can pass my ‑ComputerName, and then I can say

9
00:00:31,730 --> 00:00:35,030
10.0.0.5, which, of course, is my domain.

10
00:00:35,030 --> 00:00:39,720
And then, of course, I've got a credential, and I can say I want to use the

11
00:00:39,720 --> 00:00:43,540
$creds object. So I can press Enter here, and this will go and create me that

12
00:00:43,540 --> 00:00:48,650
kind of interactive session because I'm entering into the session. Now if I

13
00:00:48,650 --> 00:00:51,240
come out of that, I'm just going to say exit.

14
00:00:51,240 --> 00:00:56,120
I also can use New‑PSSession, which is a little bit different.

15
00:00:56,120 --> 00:00:58,610
The syntax is almost the same.

16
00:00:58,610 --> 00:01:03,990
So let me do 10.0.0.5 here, and then we'll do ‑Credential,

17
00:01:03,990 --> 00:01:07,440
and we'll say $creds, and press Enter.

18
00:01:07,440 --> 00:01:09,960
Now notice the difference between the two. When you choose

19
00:01:09,960 --> 00:01:15,080
Enter, what it does is it just creates a remote connection that

20
00:01:15,080 --> 00:01:17,940
doesn't get listed as a session here,

21
00:01:17,940 --> 00:01:21,780
a stored session, and it just lets you kind of take over

22
00:01:21,780 --> 00:01:23,880
the console on that other machine.

23
00:01:23,880 --> 00:01:25,170
So fairly straightforward.

24
00:01:25,170 --> 00:01:28,050
So that's one way of doing it. But another way of doing it,

25
00:01:28,050 --> 00:01:32,500
which I find personally for me is a little bit nicer is because I often

26
00:01:32,500 --> 00:01:37,960
want to run a command multiple times or run multiple commands in the same

27
00:01:37,960 --> 00:01:40,840
session, but not always at the same time.

28
00:01:40,840 --> 00:01:44,710
So you can see that we've got this session that was created, and of course,

29
00:01:44,710 --> 00:01:51,340
as you know, we can do Enter‑PSSession, and then I can say ‑Session. I could

30
00:01:51,340 --> 00:01:58,660
say Get‑PSSession, and remember to use the ‑Id option 10, and press Enter.

31
00:01:58,660 --> 00:02:00,650
So I'm now back into that session.

32
00:02:00,650 --> 00:02:04,670
So, if it was me as a personal choice, I would want to use

33
00:02:04,670 --> 00:02:08,440
the New‑PSSession so that I could always go in and out of

34
00:02:08,440 --> 00:02:10,290
the sessions as I need them.

35
00:02:10,290 --> 00:02:13,920
And, of course, if I just exit out of the session, I can also

36
00:02:13,920 --> 00:02:17,450
then remove the session when I finish with it.

37
00:02:17,450 --> 00:02:21,700
So it'll say ‑Session. I could say, well actually, we could just use

38
00:02:21,700 --> 00:02:26,500
Id at this point, so I could say ‑Id and ‑Id of 10 and just remove,

39
00:02:26,500 --> 00:02:31,510
which means that my PSSession objects, obviously, I've got one

40
00:02:31,510 --> 00:02:36,630
existing, so let me get rid of that one too, so 4, and then

41
00:02:36,630 --> 00:02:38,340
Get‑PSSession, I have nothing.

42
00:02:38,340 --> 00:02:42,340
So, I've cleared all of my sessions down, so fairly

43
00:02:42,340 --> 00:02:44,640
easy way of kind of managing.

44
00:02:44,640 --> 00:02:49,930
Now things change when we start to connect to multiple machines at the

45
00:02:49,930 --> 00:02:54,080
same time. Let's say we need to execute a specific command. Let's say

46
00:02:54,080 --> 00:02:58,200
we stick with our Get‑ComputerInfo command that we wish to run against

47
00:02:58,200 --> 00:03:03,020
something or we want to just make remote sessions to a whole bunch of

48
00:03:03,020 --> 00:03:04,740
machines in one go.

49
00:03:04,740 --> 00:03:08,440
So what we can do is the first approach we've got is to

50
00:03:08,440 --> 00:03:12,670
create a simple array of IP addresses like so,

51
00:03:12,670 --> 00:03:17,710
so there's my Active Directory server. I'm then going to say 10.0.0.10,

52
00:03:17,710 --> 00:03:26,320
which is my member server 1, 10.0.0.11, and then 10.0.0.12.

53
00:03:26,320 --> 00:03:28,120
So I've got four machines,

54
00:03:28,120 --> 00:03:31,120
so one is Active Directory, and then three member servers.

55
00:03:31,120 --> 00:03:35,140
So what does that look like when we kind of bring it back out as a variable?

56
00:03:35,140 --> 00:03:38,870
There's my kind of structure of servers. Now, of course, I could have 100

57
00:03:38,870 --> 00:03:43,490
servers in there. Now, when we want to create connections,

58
00:03:43,490 --> 00:03:48,440
we can say New‑PSSession because we can't really enter the session because

59
00:03:48,440 --> 00:03:51,340
it wouldn't return that back into the user interface.

60
00:03:51,340 --> 00:03:55,380
So we can say ‑ComputerName, and this time around,

61
00:03:55,380 --> 00:03:57,700
we can just pass in the variable.

62
00:03:57,700 --> 00:04:00,720
I'm going to obviously pass my credential object to make sure that I

63
00:04:00,720 --> 00:04:04,180
can connect, and I'm going to press Enter. Now notice what happened.

64
00:04:04,180 --> 00:04:09,410
It generated my four sessions automatically and connected them here

65
00:04:09,410 --> 00:04:13,760
in the list of my PowerShell session table and assigned them a

66
00:04:13,760 --> 00:04:15,540
unique identifier.

67
00:04:15,540 --> 00:04:20,350
So that means I can now go into each one, or I can invoke a

68
00:04:20,350 --> 00:04:23,370
command into any one of those sessions.

69
00:04:23,370 --> 00:04:28,750
So, this way we're able to create effectively persistent sessions

70
00:04:28,750 --> 00:04:31,940
that we can then reutilize again and again.

71
00:04:31,940 --> 00:04:36,550
So if I go back in and say Remove‑PSSession, and I'm

72
00:04:36,550 --> 00:04:42,340
going to use the Id field and say 11, 12,

73
00:04:42,340 --> 00:04:48,230
13, and 14, I should be able to then go to my sessions

74
00:04:48,230 --> 00:04:50,740
table and have nothing, which is perfect.

75
00:04:50,740 --> 00:04:56,300
Now, another option here is we can actually put them all into unique variables,

76
00:04:56,300 --> 00:05:04,540
so we don't actually need to call the unique identifier. So if I do $srv01,

77
00:05:04,540 --> 00:05:18,460
$srv02, $srv03, I can then say = New‑PSSession like so, and I can go back to my

78
00:05:18,460 --> 00:05:23,430
‑ComputerName, and then I'm not going to pass the variable because the variable

79
00:05:23,430 --> 00:05:24,700
won't really know what to do.

80
00:05:24,700 --> 00:05:37,030
So I'm going to say "10.0.0.5", "10.0.0.10", "10.0.0.11", and

81
00:05:37,030 --> 00:05:41,020
then "10.0.0.12", and then press Enter.

82
00:05:41,020 --> 00:05:42,840
Now, notice what happened.

83
00:05:42,840 --> 00:05:47,340
It kind of did a thing where it tried to kind of go through and connect

84
00:05:47,340 --> 00:05:50,580
something, and it came back and said connecting to that failed.

85
00:05:50,580 --> 00:05:52,880
It didn't quite know what to do with it, and then it tried

86
00:05:52,880 --> 00:05:56,030
to connect to the next one, and it failed, and it had a bit

87
00:05:56,030 --> 00:05:57,240
of an error that was going on.

88
00:05:57,240 --> 00:05:58,470
Why did it do that?

89
00:05:58,470 --> 00:06:03,600
Because we missed a parameter, which is clearly required, which is

90
00:06:03,600 --> 00:06:09,840
‑Credential $creds, press Enter. Now, what did it do?

91
00:06:09,840 --> 00:06:12,460
Well, it didn't error this time, which is fantastic.

92
00:06:12,460 --> 00:06:13,680
So what do we now have?

93
00:06:13,680 --> 00:06:18,470
Well, if I do Get‑PSSession, it's got four sessions that have been created.

94
00:06:18,470 --> 00:06:25,290
If I go to $srv02, you can see that my session is session 20, and if I

95
00:06:25,290 --> 00:06:29,900
say $srv03, that's going to be session 22 and 21.

96
00:06:29,900 --> 00:06:31,680
Why has it dropped it into two?

97
00:06:31,680 --> 00:06:36,530
Because I only created three variables, but I put four IP addresses.

98
00:06:36,530 --> 00:06:38,950
So when obviously we define something like this,

99
00:06:38,950 --> 00:06:41,980
we need to make sure it matches, or you're going to end up with

100
00:06:41,980 --> 00:06:45,340
all of the sessions inside one single variable,

101
00:06:45,340 --> 00:06:47,710
which actually might be the preferred approach when you're

102
00:06:47,710 --> 00:06:50,540
trying to execute commands against them.

103
00:06:50,540 --> 00:06:54,470
So the most important key here is that single connections to

104
00:06:54,470 --> 00:06:58,520
servers or workstations can be done using either entering to give

105
00:06:58,520 --> 00:07:01,470
you interactive or new to give a persistent.

106
00:07:01,470 --> 00:07:03,560
But if we're using multiple machines,

107
00:07:03,560 --> 00:07:07,590
it's often easier to create the variable or read a file that has the

108
00:07:07,590 --> 00:07:15,000
server names in and then just create new PS sessions so that we can reuse them at any point in the future.

