520 likes | 1.27k Views
Powershell. An introduction to .NET scripting & automation. Follow me @ CmdrTchort www.fianbakken.com | harald.fianbakken@ciber.com. Agenda. Scripting 101 Demo / Hands on More advanced Continue demo Q/A. Scripting 101 – What is powershell ?. Microsoft task automation framework
E N D
Powershell An introduction to .NET scripting & automation Follow me @CmdrTchort www.fianbakken.com | harald.fianbakken@ciber.com
Agenda • Scripting 101 • Demo / Hands on • More advanced • Continue demo • Q/A
Scripting 101 – What is powershell? • Microsoft task automation framework • Kinda like ‘bash’ • Built on top of .NET • Full access to COM & WMI • Addonsfor the Sys Admin • AD • Sharepoint • Azure • SQL • What it can’t do?
Scripting 101 – Variables • $ • Dynamic typed • Set-Variable • Scoping • Case insensitive • Everything is an object! $a = «I am a variable» $a = 130 $a = new-object System.Drawing.Bitmap [int]$a = 130 Set-Variable –name a –value 130 $script:myVar = «Some var»
Scripting 101 – Variables • @() = Array • @{} = Dictionary • Strings are cool • format • replace $a = @(1,2,3,4) $a = @{«key» = «value»; «key2»= «val2»;} $a = «The date is now {0}» $a –f (Get-Date).ToString(«dd-MM-yy) The date is now 23-05-11 "{0} diskettes per CD" -f (720mb/1.44mb) 500 diskettes per CD
Scripting 101 – Special characters • $ - Variable • $_ - this • $$ - Last token • $? – Return success • | - Pipeline • .. - Range • :: - Static members • # - Commenting • @ - Here Strings • & - Execute string $a = «I am a variable» ‘this’ or self. Current item in pipeline Write-Host «Blabla» $$ Start-Hack Command «Start-Hack» does not exist $? => false $text = @» LongText$(someCode) MoreText «
Scripting 101 – Help system • Get-Command • Get-Help • Get-Member Get-Command Get-*Service* CommandType Name Definition Cmdlet Get-Service Get-Service [[-Name] <String[]>] [-ComputerName Get-Help Get-Service -examples Get-Member –InputObject «SomeObject»
Scripting 101 – Control flow • If/ElseIf/Else • Logical operations • $true / $false • -lt / -gt / -equals • Try/Catch • Trap • Switch If(«Bird» -gt «word»){ Write-Host «The bird is greater than the word» } elseif(«bird –lt «word»){ Write-Host «Less than equal than the word» } else{ Write-Host «Haven’t you heard?» } try{ # Some operation } catch [System.Net.WebException]{ Write-Host $_.Exception.ToString() } catch { } $a = «Da323» switch -wildcard($a) { «D*» { «Has D something!»} «E*» { «Has E something!»} default { «Is something else»} }
Scripting 101 – Encapsulation • Scripts • Include • Execute • Parameters • Default • Required • Functions • Scriptblocks • Modules Param($path=«default», $reqParam=$(throw «Required attribute»)) function Perform($a, $b, $c){} # OR function Perform(){ param($a,$b,$c) } [ScriptBlock]$a = { Write-Host «Ok»} Invoke-Command $a
Scripting 101 – Some useful cmdlets • Get-ChildItem • Get-Member • Get-Content / Add-Content • New-Item • Convert-To* • Export-To* • Measure-Command • ForEach-object (Alias: ForEach , %) • Where-Object (Alias: Where, ?)
Scripting 101 – Pipeline • Pipes (|) • Left to right • Input to right-hand command • Explicit or implicit binding • Powerful syntax Get-Shapes | Where-Object {$_.Color -eq "Orange"} | Sort-Object Size Get-Shapes
Demo 1 – Enough chat – Let’s do some scripting! • Task1 (basics, assembly, export/import, pipes, filters ++) • Find all files of type xml (Two ways) • With size > 100kb, sorted by size • What method is the most efficient? • Add a boolean member IsBigXml to all the objects • Add a method member for printing the owner of the file • Export the result to a csv file (and then to an xml file) • Send the csv file by email to a given email-address • Make a dictionary counting how many files w/ the same name • Task2 (xml, windows, output, replace) • Open an xml file and parse the data (e.g. configuration) • Replace all words containing “HOST1” with “HOST2” • Get a list of all stopped services • Get a list of all processes and display these visually
Demo 1 – Enough chat.. • Task4 (Modules, pipes, filters) • Import a module called facebook • Play with the data • Task5 (Regex, .NET Framework) • Find all png images on a given url • Download these files to a folder
Advanced topics – Dissecting windows • Full WMI support • Get-WMIObject • Invoke-WMIMethod • Get-EventLog • Use all of .NET ‘s framework • P/Invoke (dll_import) • http://www.leeholmes.com/blog/2009/01/19/powershell-pinvoke-walkthrough get-wmiobject win32_service [System.Reflection.Assembly]::LoadFile(«C:\SomeFile.dll»)
Advanced topics – Events • New in version 2 • Respond to async notifications • Discover • Get-Member –Type Event • Subscribe • Register-ObjectEvent <$object> [-Name] [-SourceIdentifier] [-Action] • Show event Queue • Get-Event • Current subscriptions • Get-EventSubscriber
Advanced topics – Remoting • PSRemoting • ‘Kinda like ssh • Enable-PSRemoting • New-PSSession • Invoke-Command • Import-Command • Enter-PSSession
Demo 2 – More scripting • Some examples • Creating and using a com object • Remoting • Events
[Void] The end • Play with powershell • Enable Strict-Mode • Check out %WINDIR%\Diagnostics\* • http://gallery.technet.microsoft.com/scriptcenter • http://www.powergui.org/ • Questions? • Email:Harald.fianbakken@ciber.com • Twitter: @CmdrTchort • Blog / Slides: www.Fianbakken.com