1
00:00:00,040 --> 00:00:00,480
Okay,

2
00:00:00,480 --> 00:00:04,600
so the backup is now completed. Our next task now that we can

3
00:00:04,600 --> 00:00:09,690
run a script from a remote environment or a share is to do

4
00:00:09,690 --> 00:00:11,100
something a bit more sophisticated.

5
00:00:11,100 --> 00:00:15,730
So what about if we have an environment where we need to copy files to it,

6
00:00:15,730 --> 00:00:19,980
but we can't actually get to it from a UNC path approach for

7
00:00:19,980 --> 00:00:23,130
various security protocols or restrictions? Well,

8
00:00:23,130 --> 00:00:24,700
let me just clear this here.

9
00:00:24,700 --> 00:00:27,460
And what we'll do is we'll set some variables up.

10
00:00:27,460 --> 00:00:32,710
So the first thing here is to set a location variable, and we'll set this

11
00:00:32,710 --> 00:00:37,240
to the Active Directory server where all the shares are.

12
00:00:37,240 --> 00:00:42,240
And we actually have a directory here called Files.

13
00:00:42,240 --> 00:00:48,470
So let's go back to my folder structure, and we'll go here, and we'll click

14
00:00:48,470 --> 00:00:51,210
onto my server, and we'll click Files. And sure enough,

15
00:00:51,210 --> 00:00:54,330
it has a directory in here called PSTools, so these are the

16
00:00:54,330 --> 00:00:57,450
Sysinternals tools, and we want to make sure that we can copy

17
00:00:57,450 --> 00:01:01,640
those to each of the machines that we create sessions to or

18
00:01:01,640 --> 00:01:04,540
somehow get them across to those machines.

19
00:01:04,540 --> 00:01:07,430
So let's go back to our PowerShell. So the next thing I want to

20
00:01:07,430 --> 00:01:12,790
do is set the destination of where we want those files to

21
00:01:12,790 --> 00:01:15,640
actually be stored on that other machine.

22
00:01:15,640 --> 00:01:18,780
So we'll do it nice and simple. We'll create a directory called

23
00:01:18,780 --> 00:01:22,710
C:\Files, and that's where we'll store them.

24
00:01:22,710 --> 00:01:26,240
So we have our location and destination.

25
00:01:26,240 --> 00:01:28,650
Now let's do this manually to begin with.

26
00:01:28,650 --> 00:01:31,130
The first thing we can do is utilize something called

27
00:01:31,130 --> 00:01:36,440
Copy‑Item, and Copy‑Item is just a standard PowerShell

28
00:01:36,440 --> 00:01:39,240
command, so we can say ‑Path here.

29
00:01:39,240 --> 00:01:45,290
What I can then do is put in the location variable that we already

30
00:01:45,290 --> 00:01:51,020
created, and then I can do a \*, which will represent every single file in

31
00:01:51,020 --> 00:01:56,840
that directory, that PSTools one. I can then set the ‑Destination to be

32
00:01:56,840 --> 00:01:58,800
whatever the destination is going to be,

33
00:01:58,800 --> 00:02:02,320
which in this case, will be the local machine that I'm on in the

34
00:02:02,320 --> 00:02:06,840
C:\Program Files location, so that Files directory I've got.

35
00:02:06,840 --> 00:02:10,060
What I'm then going to do is tell it to recursively do this.

36
00:02:10,060 --> 00:02:11,940
So I'm going to press Enter.

37
00:02:11,940 --> 00:02:14,900
It's going to go ahead and connect to the remote location,

38
00:02:14,900 --> 00:02:17,910
grab whatever the files are, and then it will have

39
00:02:17,910 --> 00:02:20,270
copied it, but notice, no response here.

40
00:02:20,270 --> 00:02:23,500
So let's go back to my Files directory, and sure

41
00:02:23,500 --> 00:02:27,740
enough, on my C: drive, Files, PSTools,

42
00:02:27,740 --> 00:02:30,760
there's the list of files. So it copied them from the

43
00:02:30,760 --> 00:02:33,770
network share to my machine, so that's great.

44
00:02:33,770 --> 00:02:36,550
That means we just need to have a script that executes on

45
00:02:36,550 --> 00:02:38,840
each machine and copies everything over.

46
00:02:38,840 --> 00:02:41,440
But what about if there was a better way?

47
00:02:41,440 --> 00:02:46,640
What about if we could create a session that connects to a machine?

48
00:02:46,640 --> 00:02:50,670
So let me clear this a little bit. We're going to make some changes to this.

49
00:02:50,670 --> 00:02:56,810
So what we're going to do is say $session1, and we'll say

50
00:02:56,810 --> 00:03:03,100
New‑PSSession like so. We'll say ‑ComputerName, and I'm going to use

51
00:03:03,100 --> 00:03:07,840
the IP addresses of the machines that we've got.

52
00:03:07,840 --> 00:03:13,930
I'm going to set my authentication to be Credssp, and then I'll set my

53
00:03:13,930 --> 00:03:21,340
‑Credential to be $creds, and so I'm creating a new session.

54
00:03:21,340 --> 00:03:26,590
I'm now going to go back and modify the session to go to the second machine,

55
00:03:26,590 --> 00:03:32,540
and we'll create a new variable, and I'll do a third one, which will be to

56
00:03:32,540 --> 00:03:35,940
our third member server that we've got as well.

57
00:03:35,940 --> 00:03:38,910
So I'm creating three PowerShell sessions.

58
00:03:38,910 --> 00:03:46,160
If I now say Get‑PSSession, you'll see I've got a whole list of sessions

59
00:03:46,160 --> 00:03:49,740
there, but you see I've got a bunch for number 10, but the most important

60
00:03:49,740 --> 00:03:54,030
ones are 73, 74, 75, and I've got them to 10,

61
00:03:54,030 --> 00:03:57,090
11, and 12, so I have some PowerShell sessions.

62
00:03:57,090 --> 00:03:59,470
In actual fact, what I'll do just to make sure that we

63
00:03:59,470 --> 00:04:01,640
don't send it to the wrong ones,

64
00:04:01,640 --> 00:04:13,450
I'm going to say ‑Id of 70, 71, and 72, and then we'll just reissue the

65
00:04:13,450 --> 00:04:17,540
PSSession, and there we go. We've got our clean three environments.

66
00:04:17,540 --> 00:04:22,430
Now the idea is here that we need to copy those files to those machines,

67
00:04:22,430 --> 00:04:23,960
but have no way of doing it.

68
00:04:23,960 --> 00:04:28,040
So if we go back to the Copy‑Item syntax that we had,

69
00:04:28,040 --> 00:04:30,960
so we're going to use the same process again.

70
00:04:30,960 --> 00:04:37,320
Now the location is going to change because, obviously, we

71
00:04:37,320 --> 00:04:40,540
may be coming from that Files directory or we may be pulling

72
00:04:40,540 --> 00:04:42,400
stuff from a different location.

73
00:04:42,400 --> 00:04:44,320
The destination might be different.

74
00:04:44,320 --> 00:04:47,160
We may be saving it to a different directory,

75
00:04:47,160 --> 00:04:51,150
but for the purposes of this, we'll use all the same values like

76
00:04:51,150 --> 00:04:56,760
so. So we'll say "$($location)\*, which means it's going to be

77
00:04:56,760 --> 00:04:59,400
everything in that PSTools directory.

78
00:04:59,400 --> 00:05:04,720
I'm going to set the destination to be exactly the same on every single machine.

79
00:05:04,720 --> 00:05:06,940
And then, of course, we're going to recursively do

80
00:05:06,940 --> 00:05:11,170
this. Now if I was to enter here, it would literally do it locally,

81
00:05:11,170 --> 00:05:16,340
but luckily for us, we have another property called ‑ToSession.

82
00:05:16,340 --> 00:05:22,200
So I'm going to say $session1, $session1 is our training server number 1.

83
00:05:22,200 --> 00:05:25,280
I'm going to press Enter. Now you can see, it's copying a

84
00:05:25,280 --> 00:05:30,730
bunch of files, and it says From to 10.0.0.10.

85
00:05:30,730 --> 00:05:34,290
So it's now copying them to the other machine.

86
00:05:34,290 --> 00:05:37,230
If I go and repeat that to number 2,

87
00:05:37,230 --> 00:05:43,340
you can see 10.0.0.11, so these are different machines that it's copying to.

88
00:05:43,340 --> 00:05:47,130
And then if we do number 3, do the same thing,

89
00:05:47,130 --> 00:05:50,240
it's now going to copy those files through the PowerShell

90
00:05:50,240 --> 00:05:54,280
session without using UNC connections or trying to browse to

91
00:05:54,280 --> 00:05:56,200
it, and it's going to copy those files.

92
00:05:56,200 --> 00:05:59,230
So now let's flick over to one of those machines. We'll go

93
00:05:59,230 --> 00:06:02,960
to the $session3, so that's our third server, and we'll

94
00:06:02,960 --> 00:06:06,440
make sure those files are there.

95
00:06:06,440 --> 00:06:08,540
So this is our server 3.

96
00:06:08,540 --> 00:06:11,040
If I now click my folder structure here,

97
00:06:11,040 --> 00:06:14,860
and click here, C: drive, Files,

98
00:06:14,860 --> 00:06:20,730
you'll see that all of those files are copied to this machine over here. So we

99
00:06:20,730 --> 00:06:24,890
can use the same syntax, but using that ToSession option,

100
00:06:24,890 --> 00:06:29,110
it will allow us to copy the files from the one location to the other.

101
00:06:29,110 --> 00:06:29,750
Now, of course,

102
00:06:29,750 --> 00:06:32,770
the key to everything here is that when we create the

103
00:06:32,770 --> 00:06:34,840
sessions on the other machines,

104
00:06:34,840 --> 00:06:45,000
in order to get around any of the security issues that you may face, we do need to look at potentially utilizing the Credssp approach as well.

