PowerShell Notes for Professionals

GoalKicker.com (CC BY-SA)
Page 1 sur 184Lecteur de document UniversityLib

PowerShell Notes for Professionals

Programming · course

Voir tous les documents en systèmes d'exploitation et cloud

PowerShell

Notes for Professionals

PowerShell®

Notes for Professionals

100+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an unocial free book created for educational purposes and is

not aliated with ocial PowerShell® group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with PowerShell

....................................................................................................

2

.......................................................................................................................

2

Section 1.1: Allow scripts stored on your machine to run un-signed

Section 1.2: Aliases & Similar Functions

Section 1.3: The Pipeline - Using Output from a PowerShell cmdlet

Section 1.4: Calling .Net Library Methods

Section 1.5: Installation or Setup

Section 1.6: Commenting

Section 1.7: Creating Objects

....................................................................................................................

..................................................................................................................................

...............................................................................................................................................

........................................................................................................................................

........................................................................

3

........................................................................

2

Chapter 2: Variables in PowerShell

.....................................................................................................................

7

Section 2.1: Simple variable

Section 2.2: Arrays

Section 2.3: List Assignment of Multiple Variables

Section 2.4: Scope

Section 2.5: Removing a variable

..........................................................................................................................................

........................................................................................................................................................

.....................................................................................................

7

.........................................................................................................................................................

................................................................................................................................

Chapter 3: Operators

..................................................................................................................................................

9

Section 3.1: Comparison Operators

Section 3.2: Arithmetic Operators

Section 3.3: Assignment Operators

Section 3.4: Redirection Operators

Section 3.5: Mixing operand types, the type of the left operand dictates the behavior

Section 3.6: Logical Operators

Section 3.7: String Manipulation Operators

.............................................................................................................................

................................................................................................................................

...........................................................................................................................

............................................................................................................................

...................................................................................................................................

..............................................................................................................

......................................

11

Chapter 4: Special Operators

..............................................................................................................................

13

Section 4.1: Array Expression Operator

Section 4.2: Call Operation

Section 4.3: Dot sourcing operator

Chapter 5: Basic Set Operations

.....................................................................................................................

13

.........................................................................................................................................

............................................................................................................................

.........................................................................................................................

Section 5.1: Filtering: Where-Object / where / ?

Section 5.2: Ordering: Sort-Object / sort

Section 5.3: Grouping: Group-Object / group

Section 5.4: Projecting: Select-Object / select

......................................................................................................

14

..................................................................................................................

..........................................................................................................

..........................................................................................................

Chapter 6: Conditional logic

Section 6.1: if, else and else if

Section 6.2: Negation

Section 6.3: If conditional shorthand

..................................................................................................................................

.....................................................................................................................................

..................................................................................................................................................

.........................................................................................................................

18

Chapter 7: Loops

.........................................................................................................................................................

19

Section 7.1: Foreach

Section 7.2: For

Section 7.3: ForEach() Method

Section 7.4: ForEach-Object

Section 7.5: Continue

Section 7.6: Break

Section 7.7: While

Section 7.8: Do

.....................................................................................................................................................

............................................................................................................................................................

...................................................................................................................................

19

.......................................................................................................................................

...................................................................................................................................................

........................................................................................................................................................

.........................................................................................................................................................

.............................................................................................................................................................

4

5

5

6

7

7

8

8

9

9

10

10

11

11

13

Publicité

13

14

14

15

16

17

17

17

19

19

20

21

21

22

22

Chapter 8: Switch statement

................................................................................................................................

24

...........................................................................................................................................

24

Section 8.1: Simple Switch

Section 8.2: Switch Statement with CaseSensitive Parameter

Section 8.3: Switch Statement with Wildcard Parameter

Section 8.4: Switch Statement with File Parameter

Section 8.5: Simple Switch with Default Condition

Section 8.6: Switch Statement with Regex Parameter

Section 8.7: Simple Switch With Break

Section 8.8: Switch Statement with Exact Parameter

Section 8.9: Switch Statement with Expressions

...............................................................................

24

........................................................................................

.................................................................................................

...................................................................................................

............................................................................................

......................................................................................................................

..............................................................................................

......................................................................................................

Chapter 9: Strings

.......................................................................................................................................................

28

Section 9.1: Multiline string

Section 9.2: Here-string

Section 9.3: Concatenating strings

Section 9.4: Special characters

Section 9.5: Creating a basic string

Section 9.6: Format string

Chapter 10: HashTables

..........................................................................................................................................

..............................................................................................................................................

............................................................................................................................

..................................................................................................................................

...........................................................................................................................

29

..........................................................................................................................................

...........................................................................................................................................

Section 10.1: Access a hash table value by key

Section 10.2: Creating a Hash Table

Section 10.3: Add a key value pair to an existing hash table

Section 10.4: Remove a key value pair from an existing hash table

Section 10.5: Enumerating through keys and Key-Value Pairs

Section 10.6: Looping over a hash table

........................................................................................................

..........................................................................................................................

..................................................................................

.....................................................................

32

..............................................................................

Chapter 11: Working with Objects

Section 11.1: Examining an object

Section 11.2: Updating Objects

Section 11.3: Creating a new object

Section 11.4: Creating Instances of Generic Classes

...................................................................................................................

.......................................................................................................................

...............................................................................................................................

...................................................................................................................................

............................................................................................................................

................................................................................................

37

Chapter 12: PowerShell Functions

......................................................................................................................

39

Section 12.1: Basic Parameters

Section 12.2: Advanced Function

Section 12.3: Mandatory Parameters

Section 12.4: Parameter Validation

Section 12.5: Simple Function with No Parameters

...................................................................................................................................

...............................................................................................................................

........................................................................................................................

............................................................................................................................

..................................................................................................

43

Chapter 13: PowerShell Classes

...........................................................................................................................

44

Section 13.1: Listing available constructors for a class

Section 13.2: Methods and properties

Section 13.3: Constructor overloading

Section 13.4: Get All Members of an Instance

Section 13.5: Basic Class Template

Section 13.6: Inheritance from Parent Class to Child Class

.......................................................................................................................

.......................................................................................................................

...........................................................................................................

............................................................................................................................

............................................................................................

.....................................................................................

47

Chapter 14: PowerShell Modules

........................................................................................................................

48

Section 14.1: Create a Module Manifest

Section 14.2: Simple Module Example

Section 14.3: Exporting a Variable from a Module

Section 14.4: Structuring PowerShell Modules

Section 14.5: Location of Modules

Section 14.6: Module Member Visibility

.....................................................................................................................

.......................................................................................................................

...................................................................................................

49

..........................................................................................................

..............................................................................................................................

.....................................................................................................................

Chapter 15: PowerShell profiles

..........................................................................................................................

50

24

25

25

26

26

Publicité

27

27

28

28

28

29

30

31

31

31

31

32

32

34

34

35

35

39

39

41

41

44

45

45

46

46

48

48

49

49

49

Section 15.1: Create an basic profile

Chapter 16: Calculated Properties

..........................................................................................................................

50

.....................................................................................................................

51

Section 16.1: Display file size in KB - Calculated Properties

.....................................................................................

51

Chapter 17: Using existing static classes

........................................................................................................

52

Section 17.1: Adding types

Section 17.2: Using the .Net Math Class

Section 17.3: Creating new GUID instantly

...........................................................................................................................................

.....................................................................................................................

................................................................................................................

52

................................................................................................................................

54

Chapter 18: Built-in variables

Section 18.1: $PSScriptRoot

Section 18.2: $Args

Section 18.3: $PSItem

Section 18.4: $?

Section 18.5: $error

.........................................................................................................................................

......................................................................................................................................................

...................................................................................................................................................

.............................................................................................................................................................

......................................................................................................................................................

Chapter 19: Automatic Variables

........................................................................................................................

56

Section 19.1: $OFS

Section 19.2: $?

Section 19.3: $null

Section 19.4: $error

Section 19.5: $pid

Section 19.6: Boolean values

Section 19.7: $_ / $PSItem

Section 19.8: $PSVersionTable

........................................................................................................................................................

.............................................................................................................................................................

.........................................................................................................................................................

......................................................................................................................................................

.........................................................................................................................................................

......................................................................................................................................

..........................................................................................................................................

...................................................................................................................................

58

Chapter 20: Environment Variables

..................................................................................................................

59

Section 20.1: Windows environment variables are visible as a PS drive called Env:

Section 20.2: Instant call of Environment Variables with $env:

..............................................................................

59

............................................

59

Chapter 21: Splatting

................................................................................................................................................

60

Section 21.1: Piping and Splatting

Section 21.2: Passing a Switch parameter using Splatting

Section 21.3: Splatting From Top Level Function to a Series of Inner Function

Section 21.4: Splatting parameters

......................................................................................

....................................................

61

............................................................................................................................

61

...............................................................................................................................

Chapter 22: PowerShell "Streams"; Debug, Verbose, Warning, Error, Output and

Information

....................................................................................................................................................................

63

Section 22.1: Write-Output

Section 22.2: Write Preferences

..........................................................................................................................................

63

.................................................................................................................................

63

Chapter 23: Sending Email

.....................................................................................................................................

65

Section 23.1: Send-MailMessage with predefined parameters

Section 23.2: Simple Send-MailMessage

Section 23.3: SMTPClient - Mail with .txt file in body message

...................................................................................................................

66

...............................................................................

66

...............................................................................

65

Chapter 24: PowerShell Remoting

....................................................................................................................

67

Section 24.1: Connecting to a Remote Server via PowerShell

Section 24.2: Run commands on a Remote Computer

Section 24.3: Enabling PowerShell Remoting

Section 24.4: A best practise for automatically cleaning-up PSSessions

Publicité

................................................................................

...........................................................................................

...........................................................................................................

Chapter 25: Working with the PowerShell pipeline

Section 25.1: Writing Functions with Advanced Lifecycle

Section 25.2: Basic Pipeline Support in Functions

Section 25.3: Working concept of pipeline

Chapter 26: PowerShell Background Jobs

........................................................................................

....................................................................................................

...............................................................................................................

....................................................................................................

..............................................................

70

...................................................................................

71

52

52

54

54

54

54

54

56

56

56

57

57

57

58

60

60

67

67

69

71

71

72

73

Section 26.1: Basic job creation

Section 26.2: Basic job management

..................................................................................................................................

73

........................................................................................................................

73

Chapter 27: Return behavior in PowerShell

..................................................................................................

75

Section 27.1: Early exit

Section 27.2: Gotcha! Return in the pipeline

Section 27.3: Return with a value

Section 27.4: How to work with functions returns

Section 27.5: Gotcha! Ignoring unwanted output

.................................................................................................................................................

.............................................................................................................

...............................................................................................................................

....................................................................................................

75

.....................................................................................................

77

Chapter 28: CSV parsing

.........................................................................................................................................

78

Section 28.1: Basic usage of Import-Csv

Section 28.2: Import from CSV and cast properties to correct type

...................................................................................................................

78

.....................................................................

78

Chapter 29: Working with XML Files

Section 29.1: Accessing an XML File

Section 29.2: Creating an XML Document using XmlWriter()

Section 29.3: Adding snippets of XML to current XMLDocument

..................................................................................................................

...........................................................................................................................

.................................................................................

...........................................................................

82

Chapter 30: Communicating with RESTful APIs

...........................................................................................

88

......................................................................................................................

Section 30.1: Post Message to hipChat

Section 30.2: Using REST with PowerShell Objects to GET and POST many items

Section 30.3: Use Slack.com Incoming Webhooks

Section 30.4: Using REST with PowerShell Objects to Get and Put individual data

Section 30.5: Using REST with PowerShell to Delete items

..................................................................................................

.....................................................................................

89

.............................................

89

..............................................

Chapter 31: PowerShell SQL queries

.................................................................................................................

90

Section 31.1: SQLExample

Section 31.2: SQLQuery

............................................................................................................................................

90

...............................................................................................................................................

90

Chapter 32: Regular Expressions

........................................................................................................................

91

..........................................................................................................................................

..................................................................................................................................................

Section 32.1: Single match

Section 32.2: Replace

Section 32.3: Replace text with dynamic value using a MatchEvalutor

Section 32.4: Escape special characters

Section 32.5: Multiple matches

...................................................................................................................

...................................................................................................................................

................................................................

93

Chapter 33: Aliases

.....................................................................................................................................................

97

Section 33.1: Get-Alias

Section 33.2: Set-Alias

.................................................................................................................................................

97

.................................................................................................................................................

97

Chapter 34: Using the progress bar

Section 34.1: Simple use of progress bar

Section 34.2: Usage of inner progress bar

.................................................................................................................

..................................................................................................................

...............................................................................................................

98

Chapter 35: PowerShell.exe Command-Line

...............................................................................................

Publicité

100

Section 35.1: Executing a command

Section 35.2: Executing a script file

........................................................................................................................

100

.........................................................................................................................

101

Chapter 36: Cmdlet Naming

................................................................................................................................

102

Section 36.1: Verbs

Section 36.2: Nouns

.....................................................................................................................................................

102

...................................................................................................................................................

102

Chapter 37: Running Executables

....................................................................................................................

103

Section 37.1: GUI Applications

Section 37.2: Console Streams

Section 37.3: Exit Codes

..................................................................................................................................

103

.................................................................................................................................

103

............................................................................................................................................

103

Chapter 38: Enforcing script prerequisites

.................................................................................................

104

Section 38.1: Enforce minimum version of PowerShell host

..................................................................................

104

75

75

75

80

80

81

88

88

88

91

93

94

94

98

98

Section 38.2: Enforce running the script as administrator

....................................................................................

104

105

105

105

105

105

106

107

107

108

109

Chapter 39: Using the Help System

Section 39.1: Updating the Help System

Section 39.2: Using Get-Help

Section 39.3: Viewing online version of a help topic

Section 39.4: Viewing Examples

Section 39.5: Viewing the Full Help Page

Section 39.6: Viewing help for a specific parameter

Chapter 40: Modules, Scripts and Functions

.................................................................................................................

.................................................................................................................

....................................................................................................................................

..............................................................................................

...............................................................................................................................

................................................................................................................

.............................................................................................

106

.............................................................................................

107

Section 40.1: Function

Section 40.2: Script

Section 40.3: Module

Section 40.4: Advanced Functions

Chapter 41: Naming Conventions

...............................................................................................................................................

....................................................................................................................................................

.................................................................................................................................................

..........................................................................................................................

.....................................................................................................................

112

Section 41.1: Functions

...............................................................................................................................................

112

Chapter 42: Common parameters

Section 42.1: ErrorAction parameter

..................................................................................................................

113

.......................................................................................................................

113

Chapter 43: Parameter sets

...............................................................................................................................

115

Section 43.1: Parameter set to enforce the use of a parameter when a other is selected

Section 43.2: Parameter set to limit the combination of parameters

.................................................................

115

...............................

115

Chapter 44: PowerShell Dynamic Parameters

.........................................................................................

116

Section 44.1: "Simple" dynamic parameter

............................................................................................................

116

Chapter 45: GUI in PowerShell

...........................................................................................................................

118

Section 45.1: WPF GUI for Get-Service cmdlet

.......................................................................................................

118

Chapter 46: URL Encode/Decode

....................................................................................................................

120

Section 46.1: Encode Query String with [System.Web.HttpUtility]::UrlEncode()

Section 46.2: Quick Start: Encoding

Section 46.3: Quick Start: Decoding

Section 46.4: Encode Query String with [uri]::EscapeDataString()

Section 46.5: Decode URL with [uri]::UnescapeDataString()

Section 46.6: Decode URL with [System.Web.HttpUtility]::UrlDecode()

....................................................................

..............................................................................

............................................................

............................................................................