agenda for powershell (level:200) powershell introduction variable & object powershell...

45

Upload: benjamin-owens

Post on 28-Dec-2015

285 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell
Page 2: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Agenda for Powershell (Level:200) Powershell Introduction Variable & Object Powershell Operator Loop and Flow Control Function and Debug Powershell in Action

Page 3: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

What is Powershell??

Windows Powershell is a command-line shell and scripting environment that brings the power of the .NET Framework to command-line users and script writers.

It introduces a number of powerful new concepts that enables you to extend the knowledge you have gained and the scripts you have created within the Windows Command Prompt and Windows Script Host environments.

Page 4: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

New Feature in Powershell Discoverability Consistency Interactive and Scripting

Environments Object Orientation Easy Transition to Scripting

Page 5: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Powershell fundamental Revolutionary interactive shell and

scripting language Based on .NET

New set of built-in tools (~130)

New language to take advantage of .NET

An “object-based” pipeline view

Can continue to use current tools

Can continue to use current instrumentation (COM, ADSI, WMI, ADO, XML, Text, …)

Page 6: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Frequently Asked Questions

Do I need to learn .NET before I can use Powershell? No - you can continue to use existing tools

Do I need to rewrite all my existing tools? No - existing tools will run just fine

Do I need to learn the new language? No - You can easily run existing commands

without modification Many Unix and DOS commands work… try

them…

Page 7: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

To begin working…

Commands are built with logic Verb-noun

Pipeline “ | ” Some good starters

Get-Help Get-Command | more Get-Command | sort-object noun | format-

table -group noun Get-Alias | more Get-Help stop-service -detailed | more

Page 8: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Learning and Documentation

Online help is full of examples Many books and documentation are

available already Microsoft Press – Microsoft Windows

PowerShell Step By Step Manning – Windows PowerShell in Action Sams – Windows PowerShell Unleashed Sapien Press – Microsoft Windows PowerShell TechNet - Scripting with Windows PowerShell

Page 9: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

PowerShell Releases

Get v1.0 from the Download Center for: Windows XP SP2 Windows Server 2003 SP2 Windows Vista Windows Server 2008 RC0 (no need to d/l,

available as a feature) Downloading and Installing Windows PowerShell Version 1.0 http://www.microsoft.com/technet/scriptcenter/topics/msh/download.mspx

Version 2.0 is CTP http://www.microsoft.com/technet/scriptcenter/topics/winpsh/pshell2.mspx

Page 10: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

PowerShell

Page 11: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Installation Requirements Before you install Windows PowerShell, be

sure that your system has the software programs that Windows PowerShell requires. Windows PowerShell requires the following programs:• Windows XP Service Pack 2, Windows 2003

Service Pack 1, or later versions of Windows• Microsoft .NET Framework 2.0

If any version of Windows PowerShell is already installed on the computer, use Add or Remove Programs in Control Panel to uninstall it before installing a new version.

Page 12: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Session 1 EndingDemo and Q&A

Page 13: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Variable & Object

Variable Name Variable Type and Type Adaptation All Variables are Object Array Environmental Variables

Page 14: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Variable Name

You can use virtually any variable name you choose, names are not case sensitive.

But, there are illegal characters such as; ! @ # % & , . and spaces. PowerShell will throw an error if you use an illegal character.

$Microsoft $MicroSoft $microsoft are The Same!

${My English Name is #merlin@} is OK!

Page 15: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Variable Type

Powershell variable type is base on .NET Framework.

Common variable is as below: [adsi], [array], [bool], [byte], [char] [datetime], [decimal], [double] [int] or [int32], [long] [single], [scriptblock], [string] [WMI], [WMIclass], [xml]

Page 16: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Declaring Variables and Type Adaptation

$a=333 $b=“66” $c=SS$a.GetType()$b.GetType().Name

$a+$b ; $b+$a ??$b+$c ; $c+$b ??$a+$c ; $c+$a ??

Page 17: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

All Variables are Object

[int]$Age=22 $Age.GetType() $Age GetType().Name $Age | Get-Member $Title=“manager” $Title.length $Title.CompareTo()

Page 18: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Array

$RainbowColor = "red", "orange", "yellow", "green", "blue", "indigo", "violet"

$a = 3, "apple", 3.1415926, “cat“, 23 [int[]]$b = 51, 29, 88, 27,50 $b.SetValue(19, 3) $b[-1]=888 $PeopleTable = @{“Merlin Lin" = “3725-

3888"; “Linda Chen" = “0800-000-213"…}

Page 19: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Environmental Variables

Get-ChildItem Env: Creating – and Modifying --

Environment Variables $env:testv = "This is a test environment

variable.“ [Environment]::SetEnvironmentVariable("tes

tv", "VVVV", “User") [Environment]::GetEnvironmentVariable(“te

stv","User") Remove-Item Env:\testv [Environment]::SetEnvironmentVariable(“tes

tv” ,$null,"User")

Page 20: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

PSDrive

Page 21: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

PSDrive Operation

Get-PSDrive mount -Name Seting -psProvider

FileSystem -Root "C:\Documents and Settings”

mount -Name MS -PSProvider Registry -Root HKLM\Software\Microsoft

rdr -Name MS Set-Location Get-Location

Page 22: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Session 2 EndingDemo, Q&A and Break

Page 23: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Powershell Operator

Arithmetic Binary Operators +, -, *, \, %, ++, --

Assignment Operators =, +=, -=, *=, /=, %=

Logical Operators !, -not, -and, -or

String Operators +, *, -f, -replace, -match, -like

Comparison Operators -eq, -ne, -gt, –ge, -lt, –le

Page 24: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Arithmetic Binary Operators 123+789 ; 222-876 34.5*44.2 ; 13/7 123%5 $var++ ; ++$var $var = $var +

1 $var-- ; --$var $var = $var – 1

Page 25: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Assignment Operators

$var=3 $var+=3 ; $var-=3 $var*=3 ;$var/=3 ; $var%=3 $var = 0x10 echo $var 16 $var = 7.56e3 echo $var 7560 $var=7MB echo $var 7340043

(bytes)

Page 26: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Logical Operators

(7 -eq 7) -and (2 -eq 5) (7 -eq 7) -or (2 -eq 5) (9 -eq 9) -xor (4 -eq 4) ; (9 -eq 9) -xor

(4 -eq 7) (3 -eq 3) -and !(2 -eq 2) (3 -eq 3) -and -not (2 -eq 9)

Page 27: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

String Operators

-like ; -clike ; -ilike To be like as

-notlike ; -cnotlike ;-inotlike To not be like as

-match ; -cmatch ;-imatch Match

-notmatch ; -cnotmatch ; -inotmatch Not match

-contains ; -ccontains ; -icontains Include

-notcontains; -cnotcontains ;

-inotcontains

Not include

Page 28: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Comparison Operators

-le ; -cle ; -ile <= -eq; -ceq; -ieq = -ne; -cne; -ine != -gt; -cgt; -igt > -ge; -cge; -ige >= -lt; -clt; -ilt < -le; -cle; ile <=

Page 29: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Session 3 EndingDemo and Q&A

Page 30: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Loop and Flow Control

If…. elseif… else… Switch…… default ForEach ( Foreach-Object) For While Do….. While Do…..Until Break & Continue

Page 31: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

If…. elseif… else…

If (< statement 1>) { < code 1> } Elseif (< statement 2>) { < code 2> … } Else { <code n> }

Page 32: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Switch…… default

Switch [-regex|-wildcard|-exact][-casesensitive] -file <filename> (< variable >)

{ < Pattern 1> { code 1 } < Pattern 2> { code 2 } < Pattern 3> { code 3 } … Default { code n } }

Page 33: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

ForEach( Foreach-Object )ForEach ($<item or object> in $<Collection

object>) { <code> }

dir | ForEach -process { $_.length / 1024}

Page 34: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

For

For (<initial>; < statement >; < count>) {<code>

}

Page 35: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

While, do while, do until While (< statement >) {

<code> } Do { < code >

} While (< statement >) Do {<code>

} Until (<statement>)

ps. “Until” can wait something happen!!

Page 36: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Break; Continue

For ($i = 1; $i -le 10; $i++) {Write-Host $iIf ($i -eq 5) { Write-Host "BREAK!!“Break } }

ForEach ($i in 1..10) {If ($i % 2) {

Continue }$i }

Page 37: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Session 4 EndingDemo, Q&A and Break

Page 38: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Function and Debug

Script Block Function Function Arguments Function Return Values Variable Scope Debug

Page 39: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Script Block

PS > $a = { $x = 2 , $y = 3 , $x * $y } PS > &$a PS > 6

PS > $lsName = { ForEach($item in $input)

{ $item.Name } }

PS > dir | &$lsName

Page 40: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Function

PS > Function MySimpleFun { Write-Host “This is a function“ }

PS > MySimpleFunThis is a function

Page 41: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Function Arguments

PS > Function Show-Str { Write-Host $args[0] }

PS > Show-Str “Hello , First Arg!!”Hello, First Arg!!

Page 42: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Function Return Values

PS > Function AandB([int]$x=10, [int]$y=90) {>> $x + $y>> $x - $y>> $x * $y>> }>>

PS > AandB 8 210616

PS > $rst = AandB 8 2PS > $rst.Length3

Page 43: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Variable Scope

Page 44: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Debug Write-Verbose ; Write-Debug 請檢查 PowerShell Script 是否有可讀及可執行權限 請注意 PowerShell 不是完全自由語法就像 if [ “$#”

-ne 1 ] 就不能寫成 if [“$#” -ne 1] 也不能寫成 if[ “$#” -ne 1 ] . 假如執行時有錯誤訊息 , 要詳加檢查程式的語法結構 . 該空格的地方要有空格 .

要了解 $variable 才是變數的內容 , 你很有可能把 variable 當成變數的內容 .

你可能拼錯字了 , 這是很常見的 有些情況下 , 變數中的內容並不是你預期的 , 以致造成

錯誤 . 此時可在程式中可疑的地方適時的加上一些 偵錯點 , 把這些變數內容顯示出來看看

使用 Powershell debugger, 如 PowerGUI 最後 , 邏輯上的錯誤是很難找到的 , 你可能也要注意

Page 45: Agenda for Powershell (Level:200)  Powershell Introduction  Variable & Object  Powershell Operator  Loop and Flow Control  Function and Debug  Powershell

Powershell in Action

ActiveDirectoryDemos Get-DomainInfo Get-HotFixes Get-MachinesMissingHotfix Get-SoftwareFeatures