1
00:00:01,240 --> 00:00:05,340
So another approach is to actually want to execute some kind of script.

2
00:00:05,340 --> 00:00:10,360
So instead of using static commands such as Get‑ComputerInfo or hostname,

3
00:00:10,360 --> 00:00:16,290
maybe we want to execute a script into a specific set of machines such as

4
00:00:16,290 --> 00:00:19,340
our Active Directory server and our member servers.

5
00:00:19,340 --> 00:00:22,900
So I'm going to press the Start button, and type the run

6
00:00:22,900 --> 00:00:24,770
command, and then execute the run.

7
00:00:24,770 --> 00:00:29,980
And then I'm going to use my IP address of the AD server, so 10.0.0.5.

8
00:00:29,980 --> 00:00:33,560
Just click OK, and this will launch into a series of

9
00:00:33,560 --> 00:00:35,640
shared folders on that machine.

10
00:00:35,640 --> 00:00:38,480
I'm going to go into my Scripts folder, and in here, I

11
00:00:38,480 --> 00:00:41,140
have a series of PowerShell scripts.

12
00:00:41,140 --> 00:00:44,510
What I'm going to do is actually just go and right‑click here on

13
00:00:44,510 --> 00:00:48,960
Report, and I'm just going to copy that, and then I'm going to browse

14
00:00:48,960 --> 00:00:55,410
to my C: drive here, and I'll just go into my Code folder, and I'll

15
00:00:55,410 --> 00:00:58,540
just paste that PowerShell file.

16
00:00:58,540 --> 00:01:01,750
So this is a Report PowerShell script, which we'll look at in a second,

17
00:01:01,750 --> 00:01:05,680
but I just wanted to copy it locally so we could try and execute this

18
00:01:05,680 --> 00:01:08,740
against another machine, so I'm going to minimize now.

19
00:01:08,740 --> 00:01:12,040
Now what we can do is take the same command that we

20
00:01:12,040 --> 00:01:16,420
had previously. And, for example, where we have this section here,

21
00:01:16,420 --> 00:01:17,180
which is hostname,

22
00:01:17,180 --> 00:01:22,830
I'm now deleting it, ‑ScriptBlock, etc, we can actually change that to be

23
00:01:22,830 --> 00:01:28,160
‑FilePath and then point it to the file path that we want to use.

24
00:01:28,160 --> 00:01:34,140
So a specific report, PS1 file or whatever the script may be.

25
00:01:34,140 --> 00:01:37,060
Now, what I want to do here is not actually execute it

26
00:01:37,060 --> 00:01:39,020
against all of the machines for now.

27
00:01:39,020 --> 00:01:44,970
So what we'll do is a 10.0.0.5, so just against the Active Directory

28
00:01:44,970 --> 00:01:49,660
server. And then in my file path, I could put the file path.

29
00:01:49,660 --> 00:01:52,630
So, if we go back to my run option here,

30
00:01:52,630 --> 00:01:56,640
you can see it's in my C:\Code directory.

31
00:01:56,640 --> 00:02:00,340
So I minimize here. I'm going to say

32
00:02:00,340 --> 00:02:09,440
"C:\Code\Report.ps1", and I have my kind of path.

33
00:02:09,440 --> 00:02:14,030
Now, what we can do here is I can execute this here. Now you'll notice it

34
00:02:14,030 --> 00:02:19,530
comes back and says the value of FilePath needs to have a .ps1 extension, and

35
00:02:19,530 --> 00:02:22,010
that's okay, so let's just delete these extra bits.

36
00:02:22,010 --> 00:02:25,860
This is where our syntax differs between the different types of things.

37
00:02:25,860 --> 00:02:30,140
So, now I've changed it, Invoke‑Command, this single machine

38
00:02:30,140 --> 00:02:33,440
with the credentials we have in that PS1 file.

39
00:02:33,440 --> 00:02:34,850
So I'm now executing it.

40
00:02:34,850 --> 00:02:38,320
It comes back and says, oh, hold on a minute. A file already

41
00:02:38,320 --> 00:02:41,250
exists, and that's okay because this report file,

42
00:02:41,250 --> 00:02:44,920
which we'll look at in a second, is trying to generate a specific log.

43
00:02:44,920 --> 00:02:46,630
So let me just click the Start button again,

44
00:02:46,630 --> 00:02:50,230
go to my Run command. This time, we'll actually go through and

45
00:02:50,230 --> 00:02:56,720
use that specific path, \\10.0.0.5\Devices, and we'll open

46
00:02:56,720 --> 00:02:59,390
that, and you'll see, sure enough, there's a text file.

47
00:02:59,390 --> 00:03:02,140
So I'm actually going to delete this, and we'll leave this window

48
00:03:02,140 --> 00:03:06,320
open and go back to my PowerShell window, and then we'll rerun that

49
00:03:06,320 --> 00:03:12,260
same command like so, and sure enough, it now says it's created a specific file.

50
00:03:12,260 --> 00:03:18,010
So if we go back to my Devices folder, you'll see there's a Report file.

51
00:03:18,010 --> 00:03:21,940
If I click Open,

52
00:03:21,940 --> 00:03:26,850
we'll just maximize this. You'll see it gives me some

53
00:03:26,850 --> 00:03:29,750
information around the Windows desktop,

54
00:03:29,750 --> 00:03:33,450
the manufacturer, and then some details about the operating system.

55
00:03:33,450 --> 00:03:35,200
So, how does it do that?

56
00:03:35,200 --> 00:03:35,330
Well,

57
00:03:35,330 --> 00:03:39,290
let's close this down, and this time, we'll go and open

58
00:03:39,290 --> 00:03:43,390
that specific script file that we created.

59
00:03:43,390 --> 00:03:46,640
So I'm going to right‑click here, and I'm going to say

60
00:03:46,640 --> 00:03:50,430
Open with Code, which is Visual Studio Code. And sure

61
00:03:50,430 --> 00:03:52,920
enough, my Report file is now open.

62
00:03:52,920 --> 00:03:57,240
This is my PowerShell script. So you can see it has a location set

63
00:03:57,240 --> 00:04:00,590
of where we wish to save, and then I'm using some variable

64
00:04:00,590 --> 00:04:06,210
information, so this is a standard PowerShell command, so env, so

65
00:04:06,210 --> 00:04:08,440
$env is for current environment.

66
00:04:08,440 --> 00:04:11,670
And then I'm using a property called COMPUTERNAME. I'm then

67
00:04:11,670 --> 00:04:15,860
using a default PowerShell cmdlet called Get‑CimInstance to get

68
00:04:15,860 --> 00:04:20,040
information about the desktop, about the actual manufacturer,

69
00:04:20,040 --> 00:04:22,030
operating system information.

70
00:04:22,030 --> 00:04:24,470
And then, if we go to the last bit here,

71
00:04:24,470 --> 00:04:30,500
I'm saying go to this location and basically create a report log file

72
00:04:30,500 --> 00:04:35,150
that has the populated variable information broken down by these kind

73
00:04:35,150 --> 00:04:39,560
of topic areas, so desktop details, manufacturer details,

74
00:04:39,560 --> 00:04:41,640
operating system details.

75
00:04:41,640 --> 00:04:43,440
So fairly straightforward.

76
00:04:43,440 --> 00:04:48,150
So, let's go back and see how we can run that against

77
00:04:48,150 --> 00:04:50,670
multiple machines instead of just the one.

78
00:04:50,670 --> 00:04:53,040
So let me clear my section.

79
00:04:53,040 --> 00:04:55,710
Now, of course, if we go back to my command,

80
00:04:55,710 --> 00:04:59,120
you can see that I've got my PowerShell script to run locally.

81
00:04:59,120 --> 00:05:01,490
I've got my $creds. And all I want to do here is

82
00:05:01,490 --> 00:05:05,870
just change this to be $computers.

83
00:05:05,870 --> 00:05:10,170
Before we execute, I'm going to go back to my Devices directory, and I'm

84
00:05:10,170 --> 00:05:12,730
actually just going to delete the file because I don't want to have an

85
00:05:12,730 --> 00:05:16,870
error again. Click back here, and then what we'll do is go right to the

86
00:05:16,870 --> 00:05:22,560
end here and just execute. Now this is going to go ahead and try and run

87
00:05:22,560 --> 00:05:25,720
them. Now, of course, well, we can see we've got some errors to do with

88
00:05:25,720 --> 00:05:26,820
access denied.

89
00:05:26,820 --> 00:05:30,260
So what's happening here is it's trying ‑‑‑ I'm going to scroll all the way

90
00:05:30,260 --> 00:05:34,540
up. I'm going to keep scrolling back to the top here.

91
00:05:34,540 --> 00:05:39,400
It's trying to write. You can see access to the path it says is denied,

92
00:05:39,400 --> 00:05:45,230
so it's definitely connecting to the other machines, so TRAININGSRV02, which is

93
00:05:45,230 --> 00:05:49,430
one of my member servers, and it's trying to write the value to it, and then

94
00:05:49,430 --> 00:05:53,200
it's failing with some various permission issues.

95
00:05:53,200 --> 00:05:57,740
So if we just go back here to my folder,

96
00:05:57,740 --> 00:06:01,570
my Devices, sure enough, it only created the very first one.

97
00:06:01,570 --> 00:06:03,080
So why does it do that?

98
00:06:03,080 --> 00:06:03,470
Well,

99
00:06:03,470 --> 00:06:09,000
it's because it's to do with permissions when it's been executed from a remote machine.

