PowerShell

[Pages:247]PowerShell

#powershell

Table of Contents

About

1

Chapter 1: Getting started with PowerShell

2

Remarks

2

Versions

2

Examples

2

Installation or Setup

2

Windows

2

Other Platforms

3

Allow scripts stored on your machine to run un-signed

3

Aliases & Similar Functions

4

The Pipeline - Using Output from a PowerShell cmdlet

5

Commenting

6

Calling .Net Library Methods

6

Creating Objects

7

Chapter 2: ActiveDirectory module

9

Introduction

9

Remarks

9

Examples

9

Module

9

Users

9

Groups

10

Computers

10

Objects

10

Chapter 3: Aliases

12

Remarks

12

Examples

13

Get-Alias

13

Set-Alias

13

Chapter 4: Amazon Web Services (AWS) Rekognition

15

Introduction

15

Examples

15

Detect Image Labels with AWS Rekognition

15

Compare Facial Similarity with AWS Rekognition

16

Chapter 5: Amazon Web Services (AWS) Simple Storage Service (S3)

17

Introduction

17

Parameters

17

Examples

17

Create a new S3 Bucket

17

Upload a Local File Into an S3 Bucket

17

Delete a S3 Bucket

18

Chapter 6: Anonymize IP (v4 and v6) in text file with Powershell

19

Introduction

19

Examples

19

Anonymize IP address in text file

19

Chapter 7: Archive Module

21

Introduction

21

Syntax

21

Parameters

21

Remarks

21

Examples

22

Compress-Archive with wildcard

22

Update existing ZIP with Compress-Archive

22

Extract a Zip with Expand-Archive

22

Chapter 8: Automatic Variables

23

Introduction

23

Syntax

23

Examples

23

$pid

23

Boolean values

23

$null

23

$OFS

24

$_ / $PSItem

24

$?

25

$error

25

Chapter 9: Automatic Variables - part 2

26

Introduction

26

Remarks

26

Examples

26

$PSVersionTable

26

Chapter 10: Basic Set Operations

27

Introduction

27

Syntax

27

Examples

27

Filtering: Where-Object / where / ?

27

Ordering: Sort-Object / sort

28

Grouping: Group-Object / group

29

Projecting: Select-Object / select

29

Chapter 11: Built-in variables

32

Introduction

32

Examples

32

$PSScriptRoot

32

$Args

32

$PSItem

32

$?

33

$error

33

Chapter 12: Calculated Properties

34

Introduction

34

Examples

34

Display file size in KB - Calculated Properties

34

Chapter 13: Cmdlet Naming

35

Introduction

35

Examples

35

Verbs

35

Nouns

35

Chapter 14: Comment-based help

36

Introduction

36

Examples

36

Function comment-based help

36

Script comment-based help

38

Chapter 15: Common parameters

41

Remarks

41

Examples

41

ErrorAction parameter

41

-ErrorAction Continue

41

-ErrorAction Ignore

41

-ErrorAction Inquire

42

-ErrorAction SilentlyContinue

42

-ErrorAction Stop

42

-ErrorAction Suspend

43

Chapter 16: Communicating with RESTful APIs

44

Introduction

44

Examples

44

Use Incoming Webhooks

44

Post Message to hipChat

44

Using REST with PowerShell Objects to Get and Put individual data

44

Using REST with PowerShell Objects to GET and POST many items

45

Using REST with PowerShell to Delete items

45

Chapter 17: Conditional logic

46

Syntax

46

Remarks

46

Examples

46

if, else and else if

46

Negation

47

If conditional shorthand

47

Chapter 18: Creating DSC Class-Based Resources

49

Introduction

49

Remarks

49

Examples

49

Create a DSC Resource Skeleton Class

49

DSC Resource Skeleton with Key Property

49

DSC Resource with Mandatory Property

50

DSC Resource with Required Methods

50

Chapter 19: CSV parsing

52

Examples

52

Basic usage of Import-Csv

52

Import from CSV and cast properties to correct type

52

Chapter 20: Desired State Configuration

54

Examples

54

Simple example - Enabling WindowsFeature

54

Starting DSC (mof) on remote machine

54

Importing psd1 (data file) into local variable

54

List available DSC Resources

55

Importing resources for use in DSC

55

Chapter 21: Embedding Managed Code (C# | VB)

56

Introduction

56

Parameters

56

Remarks

56

Removing Added types

56

CSharp and .NET syntax

56

Examples

57

C# Example

57

Example

57

Chapter 22: Enforcing script prerequisites

59

Syntax

59

Remarks

59

Examples

59

Enforce minimum version of powershell host

59

Enforce running the script as admininstrator

59

Chapter 23: Environment Variables

61

Examples

61

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

61

Instant call of Environment Variables with $env:

61

Chapter 24: Error handling

62

Introduction

62

Examples

62

Error Types

62

Chapter 25: GUI in Powershell

64

Examples

64

WPF GUI for Get-Service cmdlet

64

Chapter 26: Handling Secrets and Credentials

66

Introduction

66

Examples

66

Prompting for Credentials

66

Accessing the Plaintext Password

66

Working with Stored Credentials

66

Encrypter

67

The code that uses the stored credentials:

67

Storing the credentials in Encrypted form and Passing it as parameter when Required

67

Chapter 27: HashTables

69

Introduction

69

Remarks

69

Examples

69

Creating a Hash Table

69

Access a hash table value by key.

69

Looping over a hash table

70

Add a key value pair to an existing hash table

70

Enumerating through keys and Key-Value Pairs

70

Remove a key value pair from an existing hash table

71

Chapter 28: How to download latest artifact from Artifactory using Powershell script (v2.0

72

Introduction

72

Examples

72

Powershell Script for downloading the latest artifcat

72

Chapter 29: Infrastructure Automation

73

Introduction

73

Examples

73

Simple script for black-box integration test of console applications

73

Chapter 30: Introduction to Pester

74

Remarks

74

Examples

74

Getting Started with Pester

74

Chapter 31: Introduction to Psake

76

Syntax

76

Remarks

76

Examples

76

Basic outline

76

FormatTaskName example

76

Run Task conditionally

77

ContinueOnError

77

Chapter 32: ISE module

78

Introduction

78

Examples

78

Test Scripts

78

Chapter 33: Loops

79

Introduction

79

Syntax

79

Remarks

79

Foreach

79

Performance

80

Examples

80

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

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download