1
00:00:00,040 --> 00:00:03,940
So let's go back into the environment, and we'll look at how we execute

2
00:00:03,940 --> 00:00:08,140
commands using both the script block and the script file.

3
00:00:08,140 --> 00:00:12,170
We'll then look at how we execute commands for a single remote session,

4
00:00:12,170 --> 00:00:17,240
including that script block again, as well as our custom PowerShell script.

5
00:00:17,240 --> 00:00:20,940
And then we'll repeat the same process for multiple machines,

6
00:00:20,940 --> 00:00:24,820
including those member servers in the Globomantics lab to be

7
00:00:24,820 --> 00:00:29,540
able to retrieve the same information.

8
00:00:29,540 --> 00:00:33,130
Now the most important thing about performing remote PowerShell is

9
00:00:33,130 --> 00:00:36,320
obviously determining how you wish to execute the commands.

10
00:00:36,320 --> 00:00:39,880
So are we going to execute a script block command or are we going

11
00:00:39,880 --> 00:00:42,550
to execute a file command? And then, of course,

12
00:00:42,550 --> 00:00:45,480
understanding whether we execute commands in single remote

13
00:00:45,480 --> 00:00:48,940
sessions or across multiple remote sessions.

14
00:00:48,940 --> 00:00:53,920
So first off, let's have a look at invoking some kind of connection.

15
00:00:53,920 --> 00:00:57,170
So we use the Invoke‑Command that we've used before,

16
00:00:57,170 --> 00:01:00,730
so Invoke‑Command. We'll pass it a ‑ComputerName,

17
00:01:00,730 --> 00:01:03,870
which in this instance will be, let's just use our Active Directory

18
00:01:03,870 --> 00:01:07,590
server again, and then we'll do the script block,

19
00:01:07,590 --> 00:01:09,100
which we've used in the past.

20
00:01:09,100 --> 00:01:12,730
But before we do script block, let me just add in the credential object here.

21
00:01:12,730 --> 00:01:16,230
So ‑Credential and $creds.

22
00:01:16,230 --> 00:01:20,220
But in our script block, we'll just use the standard one we've been using,

23
00:01:20,220 --> 00:01:24,840
which is Get‑ComputerInfo, and we'll close that out.

24
00:01:24,840 --> 00:01:28,350
So, this is going to be a single command against a single machine.

25
00:01:28,350 --> 00:01:30,870
So it goes ahead and retrieves the various

26
00:01:30,870 --> 00:01:33,480
information remotely against that machine.

27
00:01:33,480 --> 00:01:36,540
Now, if I want to go ahead and change the machine,

28
00:01:36,540 --> 00:01:39,280
let's say I want to run it against one of my member machines,

29
00:01:39,280 --> 00:01:42,910
I'm going to say 10.0.0.10, it's going to repeat the same process,

30
00:01:42,910 --> 00:01:45,850
but you can see I'm doing it one machine at a time.

31
00:01:45,850 --> 00:01:48,630
And sure enough, if I scroll a bit further up here,

32
00:01:48,630 --> 00:01:50,590
you should see the name of this machine,

33
00:01:50,590 --> 00:01:52,940
which will be my training one.

34
00:01:52,940 --> 00:01:55,230
So, clearly not my Active Directory server.

35
00:01:55,230 --> 00:01:57,790
I could repeat that process three times.

36
00:01:57,790 --> 00:02:01,750
So I can go back here and say I wish to execute that against

37
00:02:01,750 --> 00:02:04,230
number 11, wait for that to return a value,

38
00:02:04,230 --> 00:02:05,810
then go back and repeat on 12.

39
00:02:05,810 --> 00:02:07,980
So, when we use the Invoke command,

40
00:02:07,980 --> 00:02:13,300
we can connect to a machine on a single basis and just reexecute the same

41
00:02:13,300 --> 00:02:16,940
commands again, and then we get those values returned.

42
00:02:16,940 --> 00:02:17,460
Now, of course,

43
00:02:17,460 --> 00:02:21,120
the downside to that is if you've got 50 machines you need to go onto,

44
00:02:21,120 --> 00:02:23,510
it can be, obviously, a little bit more complicated.

45
00:02:23,510 --> 00:02:27,420
So, let's look at how we can run the same thing,

46
00:02:27,420 --> 00:02:33,040
but this time using various computers all stored together.

47
00:02:33,040 --> 00:02:35,910
So let's go back and create our variable like we did before.

48
00:02:35,910 --> 00:02:39,390
So you can see I've got 10.0.0.5, which is my AD server,

49
00:02:39,390 --> 00:02:42,380
and then I'm going to list my member servers,

50
00:02:42,380 --> 00:02:50,160
which is the ones we wish to execute the commands on, 11 and 10.0.0.12 like

51
00:02:50,160 --> 00:02:53,340
so. So we have four machines that we wish to utilize.

52
00:02:53,340 --> 00:02:57,140
And, of course, as before, if we do $computers, we get that list.

53
00:02:57,140 --> 00:03:02,820
Now I'm going to go back to the Invoke‑Command syntax, and we're going to say

54
00:03:02,820 --> 00:03:07,190
‑ComputerName. And then this time, we're just going to pass the computer

55
00:03:07,190 --> 00:03:10,500
variable. I'm going to pass my credential object,

56
00:03:10,500 --> 00:03:14,100
which is obviously what we need, so $creds, and then we'll do

57
00:03:14,100 --> 00:03:17,010
as script block, we'll use the same syntax,

58
00:03:17,010 --> 00:03:22,970
so Get‑ComputerInfo, and then close the script block, and I'll press Enter.

59
00:03:22,970 --> 00:03:27,460
Now notice what happens. It's going ahead saying it's going to load it. It's

60
00:03:27,460 --> 00:03:29,040
going to load it, and load it, and load it.

61
00:03:29,040 --> 00:03:32,200
Okay, see that was really quick because, obviously it's just in a network,

62
00:03:32,200 --> 00:03:36,800
but you can see that we have one of them listed here, which is let's

63
00:03:36,800 --> 00:03:40,240
scroll here, that's my Active Directory server.

64
00:03:40,240 --> 00:03:45,740
If we scroll a bit further up, you can see we've got another one.

65
00:03:45,740 --> 00:03:50,130
So what it's done is it's retrieved all of the values and just executed

66
00:03:50,130 --> 00:03:52,430
them one after that, so there's my member server 3.

67
00:03:52,430 --> 00:03:54,980
If we go a bit further, they'll be 2, and then they'll be 1.

68
00:03:54,980 --> 00:04:02,240
So it ran the command against every single one, but kind of very quickly.

69
00:04:02,240 --> 00:04:04,770
So, let's make that a little bit easier to read.

70
00:04:04,770 --> 00:04:11,140
So let's get rid of Get‑ComputerInfo, and we'll just say hostname, press Enter.

71
00:04:11,140 --> 00:04:11,610
There we go.

72
00:04:11,610 --> 00:04:16,280
And it just retrieves the hostname remotely on all of the machines. So to

73
00:04:16,280 --> 00:04:21,660
execute what would be single commands on multiple machines, it's as simple

74
00:04:21,660 --> 00:04:29,000
as passing in a computer object or a container that has a list of those machines, so fairly straightforward.

