1
00:00:00,340 --> 00:00:04,540
So once we have either a single session or multiple sessions,

2
00:00:04,540 --> 00:00:08,640
how do we execute commands on those remote computers?

3
00:00:08,640 --> 00:00:12,440
Well, for a single remote session we're able to go,

4
00:00:12,440 --> 00:00:16,470
let's say I have my admin workstation here and I've got my member server,

5
00:00:16,470 --> 00:00:21,560
so in that single remote connection I'm able to pass

6
00:00:21,560 --> 00:00:25,100
commands backwards and forwards dynamically through what's

7
00:00:25,100 --> 00:00:28,240
referred to as the WSMan connection.

8
00:00:28,240 --> 00:00:33,050
The WSMan connection is that remote connection between the two machines.

9
00:00:33,050 --> 00:00:35,100
So you can see on the left‑hand side in the

10
00:00:35,100 --> 00:00:37,660
workstation I simply type some commands,

11
00:00:37,660 --> 00:00:41,640
they ran across to the member server and then came back.

12
00:00:41,640 --> 00:00:44,340
So how does that differ for the remote servers?

13
00:00:44,340 --> 00:00:46,650
Well, let's look at our diagram a little bit differently.

14
00:00:46,650 --> 00:00:49,260
So we obviously have the Admin Workstation,

15
00:00:49,260 --> 00:00:51,800
and of course we have Member Server 01 and 02,

16
00:00:51,800 --> 00:00:55,280
and you'll see that we have a WSMan connection as well.

17
00:00:55,280 --> 00:00:59,510
So I issue a command, it runs from the one machine to the other,

18
00:00:59,510 --> 00:01:03,040
and then it runs across to the other machine as well.

19
00:01:03,040 --> 00:01:03,560
So,

20
00:01:03,560 --> 00:01:07,500
in a multiple remote session, you execute the commands from the

21
00:01:07,500 --> 00:01:10,920
single entry point on the admin workstation,

22
00:01:10,920 --> 00:01:16,580
and it will send those to the remote machines and then return values back.

23
00:01:16,580 --> 00:01:19,660
Now in order for us to execute remote commands,

24
00:01:19,660 --> 00:01:24,310
we need to understand the format or the syntax format that we need to utilize.

25
00:01:24,310 --> 00:01:28,050
The first piece of this is that we're going to use Invoke‑Command,

26
00:01:28,050 --> 00:01:33,600
the cmdlet that will say go and send this command from here to somewhere else,

27
00:01:33,600 --> 00:01:36,440
whatever that remote machine would be.

28
00:01:36,440 --> 00:01:39,980
We can also then use the script block or file path,

29
00:01:39,980 --> 00:01:41,290
as we've looked at previously,

30
00:01:41,290 --> 00:01:49,350
so either in curly brackets send a PowerShell cmdlet that already exists or a

31
00:01:49,350 --> 00:01:53,240
UNC path to a PowerShell script that we've already created.

32
00:01:53,240 --> 00:01:58,140
And then lastly we can send any optional arguments and parameters.

33
00:01:58,140 --> 00:01:59,330
So, for example,

34
00:01:59,330 --> 00:02:01,620
maybe we want to send a variable value that we've

35
00:02:01,620 --> 00:02:04,150
populated and push that down that command.

36
00:02:04,150 --> 00:02:08,200
So we have the ability to create very complex commands that

37
00:02:08,200 --> 00:02:12,240
can be ran from the admin workstation and pushed out remotely

38
00:02:12,240 --> 00:02:15,440
to all of the other machines.

39
00:02:15,440 --> 00:02:18,740
So, let's walk through a sample PowerShell script,

40
00:02:18,740 --> 00:02:21,450
because the objective here is for us to be able to

41
00:02:21,450 --> 00:02:24,840
execute PowerShell remotely on machines.

42
00:02:24,840 --> 00:02:26,010
So the first thing is here,

43
00:02:26,010 --> 00:02:30,340
I want to set a location. So the location is going to be a UNC path,

44
00:02:30,340 --> 00:02:31,950
and you may wonder why we're using that.

45
00:02:31,950 --> 00:02:36,480
We're using a shared location because I need somewhere where

46
00:02:36,480 --> 00:02:39,460
all of the machines can gain access back to,

47
00:02:39,460 --> 00:02:41,840
because I want to write some values back.

48
00:02:41,840 --> 00:02:45,340
What I'm then going to do is I'm going to set some desktop

49
00:02:45,340 --> 00:02:47,490
settings here in the PowerShell session.

50
00:02:47,490 --> 00:02:50,640
So I'm going to go and get the name of the current computer,

51
00:02:50,640 --> 00:02:55,620
and if the environment variable for COMPUTERNAME is actually empty,

52
00:02:55,620 --> 00:02:59,130
then I'm going to use the CimInstance command to get that, and then I'm going

53
00:02:59,130 --> 00:03:04,640
to go and retrieve information about the desktop itself.

54
00:03:04,640 --> 00:03:08,190
I'm also then going to retrieve the computer manufacturer details.

55
00:03:08,190 --> 00:03:13,440
Notice we're using PowerShell, just regular CimInstance commands to get this.

56
00:03:13,440 --> 00:03:16,320
And then I'm going to get the operating system version

57
00:03:16,320 --> 00:03:18,830
information as well, so I'm going to be using some commands

58
00:03:18,830 --> 00:03:20,920
called the CimInstance commands.

59
00:03:20,920 --> 00:03:24,660
This just allows me to query information on Windows

60
00:03:24,660 --> 00:03:27,540
devices and kind of present that back.

61
00:03:27,540 --> 00:03:28,720
So that's our first bit.

62
00:03:28,720 --> 00:03:31,290
So we're setting the Save To Location,

63
00:03:31,290 --> 00:03:35,450
which means I'm going to get this info and write it back somewhere, and

64
00:03:35,450 --> 00:03:38,630
I'm going to write it back to a shared location, and then I'm going to

65
00:03:38,630 --> 00:03:42,640
retrieve info about about the servers.

66
00:03:42,640 --> 00:03:46,120
Part two of this is to actually create the report file.

67
00:03:46,120 --> 00:03:49,900
So I'm actually going to say create me a report, I'm going to use the

68
00:03:49,900 --> 00:03:53,920
location variable that we already set, which is the shared drive, and then

69
00:03:53,920 --> 00:03:58,470
I'm going to create a report passing in the name option,

70
00:03:58,470 --> 00:04:01,440
which is a variable that we populated in the previous one.

71
00:04:01,440 --> 00:04:03,840
And then I'm going to say create me a new report,

72
00:04:03,840 --> 00:04:08,400
call it Device Report, and then you can see I'm adding content to this file, so

73
00:04:08,400 --> 00:04:12,160
this log file will have a section called Desktop Details,

74
00:04:12,160 --> 00:04:15,950
Manufacturer Details, and Operating System Details, and then

75
00:04:15,950 --> 00:04:21,040
write the values from the executed commands.

76
00:04:21,040 --> 00:04:23,290
Now, if we look at how this works,

77
00:04:23,290 --> 00:04:28,650
if I'm using a single kind of remote session, then what I can do

78
00:04:28,650 --> 00:04:32,370
is actually obviously invoke the command and say this is my

79
00:04:32,370 --> 00:04:34,820
computer and then run this information,

80
00:04:34,820 --> 00:04:37,720
which is great, but we're using a PowerShell script so

81
00:04:37,720 --> 00:04:38,990
this is going to look differently.

82
00:04:38,990 --> 00:04:42,760
So you can see from here that my script, notice that I'm

83
00:04:42,760 --> 00:04:45,680
using a shared location to get the script,

84
00:04:45,680 --> 00:04:49,830
remember that if you've got a script on your machine, trying to execute it

85
00:04:49,830 --> 00:04:53,780
on a remote machine is a bit more complicated because the script doesn't

86
00:04:53,780 --> 00:04:57,170
have a path to execute because it's on my machine,

87
00:04:57,170 --> 00:04:59,960
not, obviously, on the other machine.

88
00:04:59,960 --> 00:05:03,150
So to get around this we can actually create a file share,

89
00:05:03,150 --> 00:05:06,370
store the ps1 file or the PowerShell script, and then

90
00:05:06,370 --> 00:05:09,510
call that script in the Invoke‑Command.

91
00:05:09,510 --> 00:05:11,190
So that means, for example,

92
00:05:11,190 --> 00:05:14,470
I'm connecting to the Active Directory server here, and I'm setting the

93
00:05:14,470 --> 00:05:19,900
FilePath to the UNC or shared path of the PowerShell script and having it

94
00:05:19,900 --> 00:05:24,870
execute there. Now if we're doing multiple sessions,

95
00:05:24,870 --> 00:05:28,320
because obviously this is the ideal approach here, the first thing

96
00:05:28,320 --> 00:05:30,430
we'll do is define our set of computers again,

97
00:05:30,430 --> 00:05:32,690
so you can see I'm using the same variables,

98
00:05:32,690 --> 00:05:37,540
so I'm using my Member Server 01, 02, and 03 IP addresses.

99
00:05:37,540 --> 00:05:41,090
I can then say invoke the command if I'm using a ScriptBlock,

100
00:05:41,090 --> 00:05:42,450
which we've seen before.

101
00:05:42,450 --> 00:05:48,830
So how does this change with the ps1? Well, we simply say script equals, back

102
00:05:48,830 --> 00:05:53,360
to that same UNC path, and then we just use Invoke‑Command.

103
00:05:53,360 --> 00:05:55,980
So Invoke‑Command, ‑ComputerName,

104
00:05:55,980 --> 00:06:05,000
pass in the object that contains all the computers and the FilePath to the UNC, and then it will go ahead and execute that script.

