microsoft powershell tom roeder cs215 2006fa. motivation.net as a platform shell web server database...

21
Microsoft PowerShell Tom Roeder CS215 2006fa

Post on 18-Dec-2015

221 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Microsoft PowerShell

Tom Roeder

CS215 2006fa

Page 2: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Motivation

.NET as a platform shell web server database access

Native access to resources eaiser to manage than P/Invoke scripting .NET natively can use reflection idea of managed code throughout

Page 3: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: introduction

No more text: traditional style of shell scripting: the pipe

find . –name ‘*.sh’ | xargs zip -@ shfiles what does this command do? many tools for this job

sed, xargs, cut

Instead, pass objects as return values. Why? convert to text as needed richer format

Page 4: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: Cmdlets

“built-in” commands for PowerShell “verb-noun” names

eg. get-childitem (= ls) but: new-alias, new-object

single parser for parameters passed as calls to cmdlets

extensible set: can write own cmdlets Aliases

built in with (mostly) standard scripting names

Page 5: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: variable syntax

Basic syntax similar to all scripting languages $a = “value” $a = 1 + 2 $a += 137

Built-in support for types $a = [xml]“<test><a>avalue</a></test>” [int]$a = 42 or cast to arbitrary .NET object

Page 6: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: parsing

Two contexts: command context

starts with regular character tries to execute command

expression context starts with num, var, or quoted string executes as expression

Can reset mode with “( )” (Get-Date).day + 2

Page 7: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: array and hash

$a = 1,2,3,4 $a += 5..10

+ adds elements to array .. is range operator. What is $a[1..3]?

$a[1], $a[2], $a[3]

$a = @{ one=1; val = get-childitem } $a[‘val’] $a.one

Page 8: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: if/switch

if { <test> } { <true> } else { <false> } elseif { <test> } { <else> }

switch (<var>) { <val> { <case>; break } … } default case

switch –regex (<var>) { word.* { <case> } … } dropping break gives multiple matches

$_: variable referring to current <var> where { <test> }

Page 9: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: looping

while($a –lt 137) { <block> } foreach($var in get-process) { <block> }

IEnumerable support Shorten foreach to %

ls | %{ $_.Length } receive piped objects $_ as before

for { $i = 0; $i –lt 10; $i += 2} { $i } regular for loop

Page 10: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: useful operators

&, . call operator $a = “Get-childitem”; &$a # calls get-childitem . used for executing scripts in current context

-as, -is is/as in C# $a –as [int] if { $a –is [System.DateTime] } { … }

Page 11: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: functions

function [(args)] { <body> } if Param is first statement, then gives parameters arguments passed in $args input passed in $input

parameter passing on cmd line add –x 2 –y 3 add 2 3

Page 12: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Useful commands

get-member return the members of an object eg. get-member –MemberType property

or, method

Authenticode signing can use certificates to verify scripts checks the hash and returns

Page 13: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: errors

throw throw “error” same as ThrowTerminatingError in Cmdlets

trap catch exceptions trap [DivideByZeroException] { <do something> } break/continue semantics

Page 14: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: surprises

Drives C, D Env, Alias, HKLM, variable, function mount lets you create others

variable scoping private, local, script, global

Page 15: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: surprises

import/export –CSV get-unique

$(foreach ($line in get-content C:\Test1\File1.txt) {$line.tolower().split(" ")}) | sort | get-unique

get-item group $x = new-object –COM <ProgID>

eg. Outlook.Application

Page 16: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

PowerShell: surprises

calc $calc = get-process calc $calc.add_exited({write-host

$this.Processname has exited}) $calc.HasExited $calc.kill() $calc.HasExited

Page 17: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Scriptblocks

a chunk of script a type in PowerShell

$x = [scriptblock]{$y = 137}&$x;

Can be used as an EventHandler delegate this is what’s happening in the last example object parameter -> $this EventArgs parameter -> $_

Page 18: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Cmdlet creation

Subclass of System.Management. Automation.Cmdlet must have an attribute

gives “verb” and “noun” components of name overrides at least one of

BeginProcessing, ProcessRecord, EndProcessing

use make-shell command to include it adds dll to set of cmdlets requires some registry manipulation

Page 19: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Cmdlet creation

Cmdlet parameters add [Parameter] attribute

Mandatory=true|false Position=<index> … others for in-pipeline action

Return value: any object native object facilities allow inspection can use arbitrary code

Page 20: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Example in script

function MyWhere {param ( [scriptblock]$expression )begin { $matches = 0 }process {if ( &$expression ) { $_; $matches++ }}end { "Found $matches matches" }}

Page 21: Microsoft PowerShell Tom Roeder CS215 2006fa. Motivation.NET as a platform shell web server database access Native access to resources eaiser to manage

Execution Policy

AllSigned require a digital signature and prompt user must agree to run script

RemoteSigned only files from internet need to be signed default setting

Unrestricted no signing required

Attacks on AllSigned?