PowerShell nested depth of statistical functions

Source: Internet
Author: User

PowerShell nested depth of statistical functions

This article mainly introduces how PowerShell implements nested depth of statistical functions. This article shares a function that can implement nested layers of statistical script execution. For more information, see

When you call a function, PowerShell adds a nested hierarchy. When a function calls another function or a script, the nested hierarchy is also added. Today I will share a function that tells you the hierarchy of script nesting:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Function Test-NestLevel

{

$ I = 1

$ OK = $ true

Do

{

Try

{

$ Test = Get-Variable-Name Host-Scope $ I

}

Catch

{

$ OK = $ false

}

$ I ++

} While ($ OK)

 

$ I

}

When the function you call has a recursive call, the above function is very useful. Let's look at an example of the call:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Function Test-Diving

{

Param ($ Depth)

 

If ($ Depth-gt 10) {return}

 

"Diving deeper to $ Depth meters ..."

 

$ CurrentDepth = Test-NestLevel

"Calculated depth: $ currentDepth"

 

Test-Diving-depth ($ Depth + 1)

}

 

Test-Diving-depth 1

When you run Test-Diving, the function calls itself 10 times. The function uses a parameter to control the nested hierarchy, while Test-NestLevel is responsible for returning the exact depth number.

Note that there is a difference: Test-NestLevel returns an absolute nested level, and the parameter records the number of times this function is called. If Test-Diving is embedded into another function, the absolute depth and relative depth are different.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

Ps c: \> Test-Diving-Depth 1

Diving deeper to 1 meters...

Calculated depth: 1

Diving deeper to 2 meters...

Calculated depth: 2

Diving deeper to 3 meters...

Calculated depth: 3

Diving deeper to 4 meters...

Calculated depth: 4

Diving deeper to 5 meters...

Calculated depth: 5

Diving deeper to 6 meters...

Calculated depth: 6

Diving deeper to 7 meters...

Calculated depth: 7

Diving deeper to 8 meters...

Calculated depth: 8

Diving deeper to 9 meters...

Calculated depth: 9

Diving deeper to 10 meters...

Calculated depth: 10

 

Ps c :\>& {Test-Diving-Depth 1}

Diving deeper to 1 meters...

Calculated depth: 2

Diving deeper to 2 meters...

Calculated depth: 3

Diving deeper to 3 meters...

Calculated depth: 4

Diving deeper to 4 meters...

Calculated depth: 5

Diving deeper to 5 meters...

Calculated depth: 6

Diving deeper to 6 meters...

Calculated depth: 7

Diving deeper to 7 meters...

Calculated depth: 8

Diving deeper to 8 meters...

Calculated depth: 9

Diving deeper to 9 meters...

Calculated depth: 10

Diving deeper to 10 meters...

Calculated depth: 11

 

Ps c: \>

Test-NestLevel always returns the nested depth from the current Code scope to the global scope.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.