Skip to content

Instantly share code, notes, and snippets.

@rgborck
Created September 22, 2016 17:37
Show Gist options
  • Save rgborck/c3ba19b0c4dbd5f70db558a41a44f5ea to your computer and use it in GitHub Desktop.
Save rgborck/c3ba19b0c4dbd5f70db558a41a44f5ea to your computer and use it in GitHub Desktop.
This is an outline of a PowerShell class I taught to my coworkers in the fall of 2015
Powershell - Lesson 1
Configuration:
Powershell profile
Import-Module Grc
(Test it w/ `Get-GrcTenants`)
Syntax:
Variable declaration $
Powershell variables ( $true, $null, $false, etc )
Control Structures
Conditionals (-eq, -ne, -contains, etc)
Calling functions and commandlets (For
Arrays @()
Dictionary @{}
Variable Persistance - `Get-Variable | Out-String`
| operator
> Get-GrcTenants |% {Write-Host $_.Name}
Write-Host including writing variables in strings and piping variables into ConvertTo-Json and writing the result embedded in a string
Powershell - Lesson 2
Types - The type is in `[]`. The Static Member Invocation operator is `::`. The Static method name is on the Right.
[guid]::NewGuid()
Switch (and assigning it to a variable)
Literal strings: `@" "@`
ConvertTo-Json / ConvertFrom-Json (LazyJsonExample.ps1)
New-Object
Creating custom classes `add-type @" public class
Invoke-RestMethod
Invoke-WebRequest (More work to get the entire response)
Talking to RavenDb
GRC Commandlets
Powershell - Lesson 3
" vs '
` - escape character (Line breaks, including " inside a double quoted string, `n for newline)
, operator: make an array: `$array = 1, 2, 3` or `$array = ,1` https://technet.microsoft.com/en-us/library/hh847732.aspx
. - Invoke operator
& - Call operator
param ( ... )
Functions
Writing commandlets
Modules
Paging Raven Results
Powershell - Lesson 4 (Guest speaker: Doug Lewis)
DSC - Desired State Configuration
Powershell - Lesson 5
UTF-8 Encoding
Updating raven documents
Powershell - Lesson 6
Remote scripting
Configure locally to allow outgoing scripts: (Run as admin)
> cd wsman:
> Set-Item .\localhost\Client\TrustedHosts -Value * #allows remote scripts to run on any destination
Configure remotely to allow Windows Remote Management:
> winrm quickconfig
> Enable-PSRemoting
> Invoke-Command -ComputerName goby-qa -ScriptBlock { $env:computername } -Credential dev\rborck
WMI Objects
[CmdletBinding], [Parameter(ValueFromPipeline=$true)]
Other notes:
Starting and monitoring Jobs? (DSC?)
Forgiving variable usage gotchas in powershell
$doug = 5
write "$($doog+5)"
Unzip a zip file:
$zip = $shell.NameSpace("C:\Provisioning\buildagent.zip")
$zip.items() | % { $shell.Namespace("$buildAgentBasePath").copyhere($_) }
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host
AD Commandlets, SQL CmdLets, Services CmdLets, MSMQ Cmdlets
Powershells version of Lamda's
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment