1
00:00:00,140 --> 00:00:03,740
So what are some tips and tricks for creating scripts?

2
00:00:03,740 --> 00:00:05,150
So what's the first tip?

3
00:00:05,150 --> 00:00:07,210
Well, the first one is to comment the code.

4
00:00:07,210 --> 00:00:10,850
Trust me, having written lots of PowerShell scripts over the years,

5
00:00:10,850 --> 00:00:14,110
sometimes you come back to it, and you spend the first hour trying to

6
00:00:14,110 --> 00:00:16,490
figure out what you wrote because there was no comments.

7
00:00:16,490 --> 00:00:20,440
If you simply write comments at the top and comments in the code,

8
00:00:20,440 --> 00:00:24,640
anybody will be able to go through quickly and see what the code does.

9
00:00:24,640 --> 00:00:27,830
Second is to use unique variables as names.

10
00:00:27,830 --> 00:00:31,800
And just be aware, once you do this and, you know, you end up

11
00:00:31,800 --> 00:00:34,710
creating a variable, and you end up renaming the variable or

12
00:00:34,710 --> 00:00:36,660
creating it with the same name further down,

13
00:00:36,660 --> 00:00:40,460
and you spend almost a day trying to debug why the values keep

14
00:00:40,460 --> 00:00:43,500
changing, you'll soon realize why you should create unique variable

15
00:00:43,500 --> 00:00:46,290
names. And strongly typed them, if needed,

16
00:00:46,290 --> 00:00:50,040
so you can constrain the input that comes in there.

17
00:00:50,040 --> 00:00:52,750
Name them something that makes sense too. Don't

18
00:00:52,750 --> 00:00:55,060
call them variable1, 2, 3, and 4.

19
00:00:55,060 --> 00:00:56,720
Call them what they're supposed to be.

20
00:00:56,720 --> 00:01:01,620
If the variable is to contain JSON and it's from server1, call it

21
00:01:01,620 --> 00:01:06,140
server1json so you know exactly what that would be.

22
00:01:06,140 --> 00:01:06,720
Then, of course,

23
00:01:06,720 --> 00:01:10,020
you can use what's called start/stop transcript. Now we haven't talked about

24
00:01:10,020 --> 00:01:14,270
this one, but this is a way for you to, when you execute a piece of

25
00:01:14,270 --> 00:01:17,990
PowerShell, if you start the transcript and stop it,

26
00:01:17,990 --> 00:01:23,440
you'll have a output of all the logical steps that took place.

27
00:01:23,440 --> 00:01:25,540
Use try/catch.

28
00:01:25,540 --> 00:01:28,260
It's just an easy way to capture any of the thrown

29
00:01:28,260 --> 00:01:32,800
exceptions and errors and be able to control the flow that

30
00:01:32,800 --> 00:01:34,900
you're running inside those functions.

31
00:01:34,900 --> 00:01:39,940
When you call a function, try/catch it in the function try/catch.

32
00:01:39,940 --> 00:01:44,960
Also, use conditional logic, if loops, if/else, switch statements,

33
00:01:44,960 --> 00:01:48,930
whatever it would be. Use conditional logic where possible to be

34
00:01:48,930 --> 00:01:53,240
able to validate the path that it should take.

35
00:01:53,240 --> 00:01:56,780
And then, of course, lastly use loops to iterate the content.

36
00:01:56,780 --> 00:02:04,000
So if you have data coming in, look at how you loop, loop the values, and update things as you go ahead.

