Sei sulla pagina 1di 39

PowerShell

Eigil Obrestad
and Erik
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
PowerShell Tutorial
Input
System commands

Conditions
if/else
Operators
Eigil Obrestad and Erik Hjelmås
Switch/case
Where

Iteration
For
While
Foreach
August 18, 2015
Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands

Conditions
if/else
Operators
Switch/case
Where

Iteration
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only
(OUSTERHOUT, J., “Scripting: Higher-Level Programming for the 21st Century”,
Credits
IEEE Computer, Vol. 31, No. 3, March 1998, pp. 23-30.)
PowerShell

Eigil Obrestad
and Erik
WARNING!
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
The following presentation is NOT meant to be a
Input
System commands
comprehensive/complete tour of the PowerShell language.
Conditions
if/else
The purpose is to get you started with some basic program
Operators
Switch/case
constructions which you will recognize based on
Where
some-sort-of-programming-background.
Iteration
For
While
At the end of the presentation (Credits section) you will find
Foreach
pointers to more comprehensive material (reference
Math
material).
Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Practice
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args You need a Windows host running on a physical or virtual
Input machine with working access to the internet, and with
PowerShell v2.0 installed.
Input
System commands

Conditions
if/else Log in and open a terminal window, download the examples
Operators
Switch/case as we go along from
Where

Iteration http :// www . ansatt . hig . no / erikh / tutorial-powershell / FIL


For
While
Foreach
(or download all at once with filename
Math
powershell-examples.zip but remember to unblock before
Functions
unzip)
RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Hello World
Hjelmås

Variables
Arrays # hello . ps1
Structures/Classes
Command-line args

Input
Write-Host " hello world ! "
Input
System commands

Conditions execute as long as filename ends with .ps1:


if/else
Operators
Switch/case
.\ hello . ps1
Where

Iteration
For
or direct from command line cmd (DOSPROMPT)
While
Foreach powershell - command " Write-Host \ " hello world !\ " "
Math

Functions or direct from command line powershell


RegExp
PowerShell example
Write-Host " hello world ! "
PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Single Variables
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input # single-var . ps1


Input
System commands

Conditions
$firstname = " Mysil "
if/else $lastname = " Bergsprekken "
Operators
Switch/case
$fullname = " $firstname $lastname "
Where Write-Host " Hello $fullname , may I call you " `
Iteration " $firstname `? "
For
While
Foreach

Math All variables are prefixed with $


Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Exercise
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
$name = " Mysil "
System commands

Conditions
if/else
Operators
Switch/case • Use the properties and methods of this object to
Where

Iteration
⇒ find out how many characters the string contains
For
While
⇒ print the string in upper case
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Single and Double Quotes
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands
# quotes . ps1
Conditions
if/else $name = " Mysil "
Operators
Switch/case
Write-Host Hello $name
Where Write-Host " Hello $name "
Iteration Write-Host ' Hello $name '
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Arrays
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args
One-dimensional arrays:
Input
Input
System commands
# array . ps1
Conditions
if/else $os = @ ( " linux " , " windows " )
Operators
Switch/case
$os += @ ( " mac " )
Where Write-Host $os [1] # print windows
Iteration Write-Host $os # print array values
For
While
Write-Host $os . Count # length of array
Foreach

Math

Functions
Arrays are created with @(...)
RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Associative Arrays
Hjelmås

Variables
Arrays
Structures/Classes
# assoc-array . ps1
Command-line args

Input $user = @ {
Input
System commands
" frodeh " = " Frode Haug " ;
" ivarm " = " Ivar Moe "
Conditions
if/else }
Operators
Switch/case
$user += @ { " lailas " = " Laila Skiaker " }
Where Write-Host $user [ " ivarm " ] # print Ivar Moe
Iteration Write-Host @user # print array values
For
While
Write-Host $user . Keys # print array keys
Foreach Write-Host $user . Count # print length of array
Math

Functions

RegExp
Associative arrays are created with @{...} and are called
PowerShell example Hashtables in PowerShell.
PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Structures/Classes
Hjelmås

A simple object used as a struct:


Variables
Arrays
Structures/Classes
# struct . ps1
Command-line args

Input $myhost = New-Object PSObject - Property `


Input
System commands
@ { os = " " ;
Conditions
sw = @ ();
if/else user = @ {}
Operators
Switch/case
}
Where $myhost . os = " linux "
Iteration $myhost . sw += @ ( " gcc " ," flex " ," vim " )
For
While
$myhost . user += @ {
Foreach " frodeh " = " Frode Haug " ;
Math " monicas " = " Monica Strand "
Functions }
RegExp Write-Host $myhost . os
PowerShell example Write-Host $myhost . sw [2]
PowerShell Write-Host $myhost . user [ " monicas " ]
only

Credits
PowerShell

Eigil Obrestad
and Erik
Command-Line Arguments
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input All command-line arguments in the array $args


Input

Scriptname retrieved from the object $MyInvocation


System commands

Conditions
if/else
Operators # cli-args . ps1
Switch/case
Where
Write-Host " I am " $MyInvocation . InvocationName `
Iteration
For " and have " $args . Count " arguments " `
While
Foreach
" first is " $args [0]
Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Exercise
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands

Conditions
if/else
• Rewrite the previous script to only have one string (just
Operators
Switch/case
one set of double quotes (")), one at the beginning and
Where
one at the end, do not use single quotes either
Iteration
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Input From User
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands
# input-user . ps1
Conditions
if/else
Operators $something = Read-Host " Say something here "
Write-Host " you said " $something
Switch/case
Where

Iteration
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Input From the Pipeline
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
# input-pipe . ps1
System commands

Conditions $something = " $input "


if/else
Operators
Write-Host " you said " $something
Switch/case
Where

Iteration can be executed as


For
While
Foreach
Write-Output " hey hey ! " | .\ input-pipe . ps1
Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Input From Files
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands
# input-file . ps1
Conditions
if/else
Operators $file = Get-Content hello . ps1
Write-Host @file - Separator " `n "
Switch/case
Where

Iteration
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Input from System Commands
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input # input-commands . ps1
System commands

Conditions $name =( Get-WmiObject W in32_ Oper atin gSyst em ). Name


if/else
Operators $kernel =( Get-WmiObject `
Switch/case
Where
W i n 32_O perat ingS yste m ). Version
Iteration
Write-Host " I am running on $name , version " `
For " $kernel in $ ( Get-Location ) "
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
if/else
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input # if . ps1
System commands

Conditions if ( $args . Length - ne 1) {


if/else
Operators Write-Host " usage : " `
Switch/case
Where
$MyInvocation . InvocationName `
Iteration
" < argument > "
For }
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Comparison
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input Operator Meaning
System commands
−lt Less than
Conditions
if/else −gt Greater than
Operators
Switch/case −le Less than or equal to
Where

Iteration
−ge Greater than or equal to
For
While
−eq Equal to
Foreach −ne Not equal to
Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Boolean
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands
Operator Meaning
Conditions
if/else
−not Not
Operators
Switch/case
! Not
Where
−and And
Iteration
For −or Or
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik # if-num-string . ps1
Hjelmås

if ( $args . Count - ne 2) {
Variables
Arrays Write-Host " usage : " `
Structures/Classes
Command-line args
$MyInvocation . InvocationName `
" < argument > < argument > "
Input
Input exit 0
System commands
} elseif ( $args [0] - gt $args [1]) {
Conditions Write-Host $args [0] " larger than " $args [1]
} else {
if/else
Operators
Switch/case
Where
Write-Host $args [0] " smaller than or " `
" equal to " $args [1]
Iteration
For }
While
Foreach
if ( Test-Path $args [0]) {
if (!( Get-Item $args [0]). PSIsContainer ) {
Math
Write-Host $args [0] " is a file "
Functions
}
RegExp
PowerShell example
}
PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Boolean example
Hjelmås

Variables
Arrays
# if-bool . ps1
Structures/Classes

if ((1 - eq 2) - and (1 - eq 1) - or (1 - eq 1)) {


Command-line args

Input
Input
Write-Host " And has precedence "
System commands } else {
Conditions Write-Host " Or has precedence "
if/else
Operators
}
Switch/case
Where
# force OR precedence :
Iteration
For
While if ((1 - eq 2) - and ((1 - eq 1) - or (1 - eq 1))) {
Write-Host " And has precedence "
Foreach

Math
} else {
Functions Write-Host " Or has precedence "
RegExp }
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Switch/Case
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args # switch . ps1
Input
Input
System commands
$short = @ { yes = " y " ; nope = " n " }
Conditions
$ans = Read-Host
if/else switch ( $ans ) {
Operators
Switch/case
yes { Write-Host " yes " }
Where nope { Write-Host " nope " ; break }
Iteration { $short . ContainsKey ( " $ans " )} `
For
While
{ Write-Host $short [ $ans ] }
Foreach default { Write-Host " $ans `??? " }
Math }
Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Where/Where-Object
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands

Conditions # where . ps1


if/else
Operators
Switch/case Get-ChildItem | Where-Object { $_ . Length - gt 1 KB }
Where

Iteration
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Exercise
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands
• Use Get-Process and Where-Object to
Conditions
if/else ⇒ list all powershell processes
Operators
Switch/case ⇒ store the process table in an array $procs
Where

Iteration
⇒ list all processes with a working set greater than
For
While
10MB
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
For loop
Hjelmås

Variables # for . ps1


Arrays
Structures/Classes
Command-line args for ( $i =1; $i-le3 ; $i ++) {
Input Write-Host " $i "
Input
System commands
}
Conditions
if/else # something more useful :
Operators
Switch/case
Where $file = Get-ChildItem
Iteration for ( $i =0; $i-lt$file . Count ; $i ++) {
For
While
if (!( Get-Item $file [ $i ]). PSIsContainer ) {
Foreach Write-Host $file [ $i ]. Name " is a file "
Math } else {
Functions Write-Host $file [ $i ]. Name " is a directory "
RegExp }
PowerShell example }
PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
While
Hjelmås
# while . ps1
Variables
Arrays while ( $i - le 3) {
Write-Host $i
Structures/Classes
Command-line args

Input $i ++
Input }
System commands

Conditions
if/else
# something more useful :
Operators

$file = Get-ChildItem
Switch/case
Where

Iteration $i =0
For while ( $i - lt $file . Count ) {
if (!( Get-Item $file [ $i ]). PSIsContainer ) {
While
Foreach

Math Write-Host $file [ $i ]. Name " is a file "


Functions
} else {
Write-Host $file [ $i ]. Name " is a directory "
RegExp
PowerShell example }
PowerShell $i ++
only }
Credits
PowerShell

Eigil Obrestad
and Erik
Foreach loop
Hjelmås

Variables # foreach . ps1


Arrays
Structures/Classes
Command-line args foreach ( $i in Get-ChildItem ) {
Input Write-Host $i . Name
Input
System commands
}
Conditions
if/else # with associative arrays
Operators
Switch/case
Where $user = @ {
Iteration " frodeh " = " Frode Haug " ;
For
While
" monicas " = " Monica Strand " ;
Foreach " ivarm " = " Ivar Moe "
Math }
Functions foreach ( $key in $user . Keys ) {
RegExp Write-Host $user [ $key ]
PowerShell example }
PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
ForEach
Hjelmås If we want to read from the pipeline and do stuff object by
Variables
object:
Arrays
Structures/Classes # foreach-pipe . ps1
Command-line args

Input foreach ( $i in $input ) {


Input
System commands $foo += @ ( $i )
Conditions }
if/else Write-Host " size of foo is " $foo . Count
Operators
Switch/case
Where
or
Iteration
For
While
# f o r ea c h -o b ject-pipe . ps1
Foreach

Math $input | ForEach-Object {


Functions $foo += @ ( $_ )
RegExp
}
PowerShell example Write-Host " size of foo is " $foo . Count
PowerShell
only
$ Get-ChildItem | ./ foreach-object-pipe . ps1
Credits
size of foo is 20
PowerShell

Eigil Obrestad
and Erik
Operators
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args
Operator Meaning
Input
Input + Add
System commands
− Subtract
Conditions
if/else
* Multiply
Operators
Switch/case / Divide
Where
% Modulus
Iteration
For
While
Foreach # math . ps1
Math

Functions Write-Host " 3+5 is " (3+5)


RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Functions
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input # func . ps1


Input
System commands

Conditions # declare :
if/else function add ( $a , $b ) {
Operators
Switch/case Write-Host " $a + $b is " ( $a + $b )
Where
}
Iteration # use :
For
While add 5.12 2.56
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Regular expressions intro 1/5
Hjelmås

Variables
Arrays
Structures/Classes
Special/Meta-characters:
\ | ( ) [ ] { } ˆ $ * + ? .
Command-line args

Input
Input
System commands These have to be protected with \, e.g.
Conditions
if/else
http://www\.hig\.no
Operators
Switch/case
Where To match c:\temp, you need to use the regex
Iteration c:\\temp. As a string in C++ source code, this
For
While regex becomes "c:\\\\temp". Four backslashes
Foreach
to match a single one indeed.
Math

Functions

RegExp (from http:


PowerShell example
//www.regular-expressions.info/characters.html):
PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Regular expressions intro 2/5
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input Describing characters:


Input
System commands

Conditions
if/else
Operator Meaning
Operators
Switch/case
. Any single character
Where
[abcd] One of these characters
Iteration
For
[ˆabcd] Any one but these characters
While
Foreach
[a-zA-Z0-9] A character in these ranges
Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Regular expressions intro 3/5
Hjelmås

Variables
Arrays
Structures/Classes
Grouping:
Command-line args

Input
Input Operator Meaning
()
System commands

Conditions
Group
if/else | OR
Operators
Switch/case
Where

Iteration Anchoring:
For
While
Foreach
Operator Meaning
Math
ˆ Beginning of line
Functions

RegExp
$ End of line
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Regular expressions intro 4/5
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args
Repetition operators/Modifiers/Quantifiers:
Input
Input
System commands
Operator Meaning
Conditions
if/else ? 0 or 1 time
*
Operators
Switch/case 0 or more times
+
Where
1 or more times
Iteration
For {N} N times
{N,}
While
Foreach At least N times
Math {N,M} At least N but not more than M
Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Regular expressions intro 5/5
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands

Conditions
Finding URLs in HTML:
if/else (mailto|http)://[ˆ"]*
Operators
Switch/case
Where Each line should be an email address:
Iteration ˆ[A-Za-z0-9._-]+@[A-Za-z0-9.-]+$
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
PowerShell example
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args # regexp . ps1
Input
Input
System commands
$input | ForEach-Object {
Conditions
if ( $_ - match
if/else " ˆ[ A-Za-z0-9 . _- ]+ @ ([ A-Za-z0-9 .-]+) $ " ) {
Operators
Switch/case
Write-Host " Valid email " , $matches [0]
Where Write-Host " Domain is " , $matches [1]
Iteration } else {
For
While
Write-Host " Invalid email address ! "
Foreach }
Math }
Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Advanced stuff
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args

Input
Input
System commands

Conditions See the complete Mastering PowerShell book at


if/else
Operators
Switch/case
http://powershell.com/cs/blogs/ebook/
Where

Iteration
for much more of what you can do with PowerShell
For
While
Foreach

Math

Functions

RegExp
PowerShell example

PowerShell
only

Credits
PowerShell

Eigil Obrestad
and Erik
Credits
Hjelmås

Variables
Arrays
Structures/Classes
Command-line args
http://refcardz.dzone.com/refcardz/windows-powershell
Input http://powershell.com/cs/blogs/ebook/
Input http://technet.microsoft.com/en-us/library/ee692948.aspx
System commands
http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_
Conditions Quoting_and_Escape_Sequences
if/else
Operators
http://dmitrysotnikov.wordpress.com/2008/11/26/input-gotchas/
Switch/case http://stackoverflow.com/questions/59819/
Where
how-do-i-create-a-custom-type-in-powershell-for-my-scripts-to-use
Iteration http://www.powershellpro.com/powershell-tutorial-introduction/
For
http://en.wikipedia.org/wiki/Windows_PowerShell
While
Foreach http://www.johndcook.com/powershell.html
http://www.regular-expressions.info/
Math
OUSTERHOUT, J., “Scripting: Higher-Level Programming for the 21st Century”,
Functions IEEE Computer, Vol. 31, No. 3, March 1998, pp. 23-30.)
RegExp
PowerShell example

PowerShell
only

Credits

Potrebbero piacerti anche