Transcription of PowerShell
1 PowerShell #powershellTable of ContentsAbout1 Chapter 1: Getting started with PowerShell2 Remarks2 Versions2 Examples2 Installation or Setup2 Windows2 Other Platforms3 Allow scripts stored on your machine to run un-signed3 Aliases & Similar Functions4 The Pipeline - Using Output from a PowerShell cmdlet5 Commenting6 Calling .Net Library Methods6 Creating Objects7 Chapter 2: ActiveDirectory module9 Introduction9 Remarks9 Examples9 Module9 Users9 Groups10 Computers10 Objects10 Chapter 3: Aliases12 Remarks12 Examples13 Get-Alias13 Set-Alias13 Chapter 4: Amazon Web Services (AWS) Rekognition15 Introduction15 Examples15 Detect Image Labels with AWS Rekognition15 Compare Facial Similarity with AWS Rekognition16 Chapter 5.
2 Amazon Web Services (AWS) Simple Storage Service (S3)17 Introduction17 Parameters17 Examples17 Create a new S3 Bucket17 Upload a Local File Into an S3 Bucket17 Delete a S3 Bucket18 Chapter 6: Anonymize IP (v4 and v6) in text file with Powershell19 Introduction19 Examples19 Anonymize IP address in text file19 Chapter 7: Archive Module21 Introduction21 Syntax21 Parameters21 Remarks21 Examples22 Compress-Archive with wildcard22 Update existing ZIP with Compress-Archive22 Extract a Zip with Expand-Archive22 Chapter 8: Automatic Variables23 Introduction23 Syntax23 Examples23$pid23 Boolean values23$null23$OFS24$_ / $PSItem24$?
3 25$error25 Chapter 9: Automatic Variables - part 226 Introduction26 Remarks26 Examples26$PSVersionTable26 Chapter 10: Basic Set Operations27 Introduction27 Syntax27 Examples27 Filtering: Where-Object / where / ?27 Ordering: Sort-Object / sort28 Grouping: Group-Object / group29 Projecting: Select-Object / select29 Chapter 11: Built-in variables32 Introduction32 Examples32$PSScriptRoot32$Args32$PSItem3 2$?33$error33 Chapter 12: Calculated Properties34 Introduction34 Examples34 Display file size in KB - Calculated Properties34 Chapter 13: Cmdlet Naming35 Introduction35 Examples35 Verbs35 Nouns35 Chapter 14: Comment-based help36 Introduction36 Examples36 Function comment-based help36 Script comment-based help38 Chapter 15: Common parameters41 Remarks41 Examples41 ErrorAction parameter41-ErrorAction Continue41-ErrorAction Ignore41-ErrorAction Inquire42-ErrorAction SilentlyContinue42-ErrorAction Stop42-ErrorAction Suspend43 Chapter 16.
4 Communicating with RESTful APIs44 Introduction44 Examples44 Use Incoming Webhooks44 Post Message to hipChat44 Using REST with PowerShell Objects to Get and Put individual data44 Using REST with PowerShell Objects to GET and POST many items45 Using REST with PowerShell to Delete items45 Chapter 17: Conditional logic46 Syntax46 Remarks46 Examples46if, else and else if46 Negation47If conditional shorthand47 Chapter 18: Creating DSC Class-Based Resources49 Introduction49 Remarks49 Examples49 Create a DSC Resource Skeleton Class49 DSC Resource Skeleton with Key Property49 DSC Resource with Mandatory Property50 DSC Resource with Required Methods50 Chapter 19: CSV parsing52 Examples52 Basic usage of Import-Csv52 Import from CSV and cast properties to correct type52 Chapter 20.
5 Desired State Configuration54 Examples54 Simple example - enabling WindowsFeature54 Starting DSC (mof) on remote machine54 Importing psd1 (data file) into local variable54 List available DSC Resources55 Importing resources for use in DSC55 Chapter 21: Embedding Managed Code (C# | VB)56 Introduction56 Parameters56 Remarks56 Removing Added types56 CSharp and .NET syntax56 Examples57C# Example57 Chapter 22: Enforcing script prerequisites59 Syntax59 Remarks59 Examples59 Enforce minimum version of PowerShell host59 Enforce running the script as admininstrator59 Chapter 23: Environment Variables61 Examples61 Windows environment variables are visible as a PS drive called Env:61 Instant call of Environment Variables with $env:61 Chapter 24: Error handling62 Introduction62 Examples62 Error Types62 Chapter 25: GUI in Powershell64 Examples64 WPF GUI for Get-Service cmdlet64 Chapter 26.
6 Handling Secrets and Credentials66 Introduction66 Examples66 Prompting for Credentials66 Accessing the Plaintext Password66 Working with Stored Credentials66 Encrypter67 The code that uses the stored credentials:67 Storing the credentials in Encrypted form and Passing it as parameter when Required67 Chapter 27: HashTables69 Introduction69 Remarks69 Examples69 Creating a Hash Table69 Access a hash table value by over a hash table70 Add a key value pair to an existing hash table70 Enumerating through keys and Key-Value Pairs70 Remove a key value pair from an existing hash table71 Chapter 28: How to download latest artifact from Artifactory using PowerShell script ( Script for downloading the latest artifcat72 Chapter 29: Infrastructure Automation73 Introduction73 Examples73 Simple script for black-box integration test of console applications73 Chapter 30.)
7 Introduction to Pester74 Remarks74 Examples74 Getting Started with Pester74 Chapter 31: Introduction to Psake76 Syntax76 Remarks76 Examples76 Basic outline76 FormatTaskName example76 Run Task conditionally77 ContinueOnError77 Chapter 32: ISE module78 Introduction78 Examples78 Test Scripts78 Chapter 33: Loops79 Introduction79 Syntax79 Remarks79 Foreach79 Performance80 Examples80 For80 Foreach80 While81 ForEach-Object81 Basic usage82 Advanced usage82Do83 ForEach() Method83 Continue84 Break84 Chapter 34: Modules, Scripts and Functions86 Introduction86 Examples86 Function86 Demo86 Script87 Demo87 Module88 Demo88 Advanced Functions88 Chapter 35: MongoDB92 Remarks92 Examples92 MongoDB with C# driver using PowerShell92I have 3 sets of array in Powershell92 Chapter 36: Naming Conventions94 Examples94 Functions94 Chapter 37: Operators95 Introduction95 Examples95 Arithmetic Operators95 Logical Operators95 Assignment Operators95 Comparison Operators96 Redirection Operators96 Mixing operand types.
8 The type of the left operand dictates the Manipulation Operators98 Chapter 38: Package management99 Introduction99 Examples99 Find a PowerShell module using a pattern99 Create the default PowerShell Module Reposity99 Find a module by name99 Install a Module by name99 Uninstall a module my name and version99 Update a module by name99 Chapter 39: Parameter sets101 Introduction101 Examples101 Simple parameter sets101 Parameterset to enforce the use of a parmeter when a other is set to limit the combination of parmeters102 Chapter 40: PowerShell "Streams".
9 Debug, Verbose, Warning, Error, Output and Information103 Remarks103 Examples103 Write-Output103 Write Preferences103 Chapter 41: PowerShell Background Jobs105 Introduction105 Remarks105 Examples105 Basic job creation105 Basic job management106 Chapter 42: PowerShell Classes108 Introduction108 Examples108 Methods and properties108 Listing available constructors for a class108 Constructor overloading110 Get All Members of an Instance110 Basic Class Template110 Inheritance from Parent Class to Child Class111 Chapter 43: PowerShell Dynamic Parameters112 Examples112"Simple" dynamic parameter112 Chapter 44: PowerShell Functions114 Introduction114 Examples114 Simple Function with No Parameters114 Basic Parameters114 Mandatory Parameters115 Advanced Function116 Parameter Validation117 ValidateSet117 ValidateRange118 ValidatePattern118 ValidateLength118 ValidateCount118 ValidateScript118 Chapter 45.
10 PowerShell Modules120 Introduction120 Examples120 Create a Module Manifest120 Simple Module Example120 Exporting a Variable from a Module121 Structuring PowerShell Modules121 Location of Modules122 Module Member Visibility122 Chapter 46: PowerShell profiles123 Remarks123 Examples123 Create an basic profile123 Chapter 47: PowerShell Remoting125 Remarks125 Examples125 enabling PowerShell Remoting125 Only for non-domain environments125 enabling Basic Authentication125 Connecting to a remote server via PowerShell126 Run commands on a remote Computer126 Remoting serialization warning127 Argument Usage128A best practise for automatically cleaning-up PSSessions128 Chapter 48: PowerShell sql queries130 Introduction130 Parameters130 Remarks130 Examples132 SQLE xample132 SQLQ uery132 Chapter 49.