powershell
Download
Skip this Video
Download Presentation
Powershell

Loading in 2 Seconds...

play fullscreen
1 / 18

Powershell - PowerPoint PPT Presentation


  • 157 Views
  • Uploaded on

Powershell. An introduction to .NET scripting & automation. Follow me @ CmdrTchort www.fianbakken.com | [email protected] Agenda. Scripting 101 Demo / Hands on More advanced Continue demo Q/A. Scripting 101 – What is powershell ?. Microsoft task automation framework

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Powershell' - kailey


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
powershell

Powershell

An introduction to .NET scripting & automation

Follow me @CmdrTchort

www.fianbakken.com | [email protected]

agenda
Agenda
  • Scripting 101
  • Demo / Hands on
  • More advanced
  • Continue demo
  • Q/A
scripting 101 what is powershell
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
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 variables1
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
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
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
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
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
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
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
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
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
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
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
Advanced topics – Remoting
  • PSRemoting
  • ‘Kinda like ssh
  • Enable-PSRemoting
  • New-PSSession
  • Invoke-Command
  • Import-Command
  • Enter-PSSession
demo 2 more scripting
Demo 2 – More scripting
  • Some examples
    • Creating and using a com object
    • Remoting
    • Events
void the end
[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:[email protected]
  • Twitter: @CmdrTchort
  • Blog / Slides: www.Fianbakken.com
ad