26
-1- Windows PowerShell 呂呂呂 呂呂呂呂呂呂呂呂呂 呂呂呂呂 http://edu.uuu.com.tw

0220 Windows server 2008 PowerShell

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 0220 Windows server 2008 PowerShell

-1-

Windows PowerShell

呂政周精誠恆逸教育訓練處 資深講師http://edu.uuu.com.tw

Page 2: 0220 Windows server 2008 PowerShell

-2-

課程大綱課程大綱• Windows PowerShell 簡介• Windows PowerShell 的優點• Windows PowerShell 的架構• 使用 Windows PowerShell Cmdlets• Windows PowerShell Provider• 使用 PowerShell Script• 使用 WMI• 使用 Active Directory• 使用檔案系統• 使用 Registry• 管理 Exchange 2007

Page 3: 0220 Windows server 2008 PowerShell

Windows PowerShell 簡介• Windows PowerShell 是微軟新創的命令列「殼層」和 script 語言

• script 語言特性– 簡單、易學、易用– 語法和結構通常比較簡單– 開發者可快速完成程式編寫工作– 直譯式的執行方式 , 不需編譯

-3-

Page 4: 0220 Windows server 2008 PowerShell

Windows PowerShell 的優點• 以 .NET 技術為基礎,並且與現有的 WSH 保持回溯

相容• 可存取 .NET CLR ,也可使用現有的 COM 技術 • 提供超過 130 種的命令列工具 ( 稱為 "cmdlet") ,

用來執行一般系統管理工作– 管理服務、處理序、事件記錄檔、憑證、登錄,以及使用 WMI 等

• 支援現有的指令碼語言和現有的命令列工具,以及多個 Windows 版本– Windows XP 、 Windows Server 2003 、 Windows Vista

以及 Windows Server 2008

-4-

Page 5: 0220 Windows server 2008 PowerShell

Windows PowerShell 的優點 (續 )

• 在命令列剖析複雜的運算式及操作 .NET Framework 物件– 包括以管道輸送物件,協助 IT 專業人員更有效率的工作

• 管理不同存放區和不同格式的 Windows 資料– ADSI 、 WMI 、 COM 物件、 ADO 、 HTML 和 XML 的標準

公用程式

• 可擴充的介面,讓獨立軟體廠商和企業開發人員建立自訂的 cmdlet ,符合應用程式和系統管理的獨特需求。

-5-

Page 6: 0220 Windows server 2008 PowerShell

Windows PowerShell Overview

Command-Command-Line Line

InterfaceInterfaceExchange Exchange Snap-InSnap-In

Task Task AutomatioAutomatio

nn

Bulk Bulk OperationsOperations

Extends Extends ConsoleConsole

Page 7: 0220 Windows server 2008 PowerShell

下載 PowerShell

• Windows Server 2008 是第一個內建 Windows PowerShell 的 Windows 系統– 預設並未安裝

• Windows 其他系統亦可下載安裝– Windows Vista x86 http://www.microsoft.com/downloads/details.aspx?

displaylang=zh-tw&FamilyID=c6ef4735-c7de-46a2-997a-ea58fdfcba63

– Windows Vista x64 http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=af37d87d-5de6-4af1-80f4-740f625cd084

– Windows XP Service Pack 2 x86 http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=30125a46-b97c-4704-aa10-605e809d5933

– Windows Server 2003 Service Pack 1 x86 http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=c61fb27b-e71c-4ecf-9d2c-9b299b149490

-7-

Page 8: 0220 Windows server 2008 PowerShell

安裝 PowerShell• 執行 PowerShell 需要 Microsoft .NET v2.0.50727 版

以上的環境– Windows Vista 、 Windows Server 2008 已內建符合要

求 .NET 執行環境– Windows Server 2003 SP1 或 Windows XP SP2 就需要自

行下載安裝 .NET 執行環境• Microsoft .NET 2.0 http://www.microsoft.com/downloads/details.aspx?

displaylang=zh-tw&FamilyID=0856EACB-4362-4B0D-8EDD-AAB15C5E04F5

• Microsoft .NET 3.0 http://www.microsoft.com/downloads/details.aspx?familyid=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=zh-tw

• 安裝後,可以在『開始』功能表看到『 Windows PowerShell 』資料夾,裡面有執行 MSH 的捷徑。只要執行 MSH 的捷徑,就可以開啟 MSH 的執行環境。

-8-

Page 9: 0220 Windows server 2008 PowerShell

使用 PowerShell

-9-

PowerShell 的第一次接觸

Page 10: 0220 Windows server 2008 PowerShell

-10-

Windows PowerShell 可以視為 Cmd 命令提示字元的延伸,許多 Cmd 或指令都可以繼續在 Windows PowerShell 執行,而且大部分的命令列工具程式也都能繼續在 Windows PowerShell 使用 ; 例如 cd/chdir 、 dir 、cls 、 ls 、 ipconfig 或是 ServerManagerCmd … 等 . Windows PowerShell 不分字母大小寫,因此 dir 和 Dir 都會視為相同的指令。

Page 11: 0220 Windows server 2008 PowerShell

Cmdlet • Windows PowerShell 雖然內建了若干指令,但重要及有用的功能是來自於外加的工具程式。例如 Cmd 雖然內建了若干指令 ( 例如 dir 、 cls 等 ) ,但如果無法執行外加的工具程式( 例如 IPConfig 、 Net 、 NetSH 、 ServerManagerCmd 等等 ) , Cmd 就失去價值及擴充的能力了。 Windows PowerShell 也是如此。

• Windows PowerShell 有自己的工具程式規格,而 Windows PowerShell 的工具程式稱為 cmdlet 。

-11-

Page 12: 0220 Windows server 2008 PowerShell

Cmdlet 特性• 名稱是由兩個部分所組成,並以橫線連結這兩個部分:前為動作、後為動作的標的– 例如 Get-Help 能取得線上說明 Get-Process 能取得行程資訊

• PowerShell 1.0 提供了 129 個 cmdlet• 提供了一個可列出所有 cmdlet 的 cmdlet

– Get-Command 這個 cmdlet 會列出所有 cmdlet 的指令類型 (都是 Cmdlet) 、名稱,以及節錄的語法說明 .

-12-

Page 13: 0220 Windows server 2008 PowerShell

Get-Help

• 如果想要知道某個 cmdlet 的使用說明,利用 Get-Help cmdlet 或以下 3 種方法,都可以達到相同的目的– Get-Help <cmdlet 名稱 >– Help <cmdlet 名稱 >– <cmdlet 名稱 > -?

-13-

Page 14: 0220 Windows server 2008 PowerShell

Get-Help ( 續 )

• Get-Help ( 或 Help) 還可以透過萬用字元列出相關主題– Get-Help * →列出所有說明主題– Get-Help set-* →列出所有 set- 開頭的主題– Get-Help *object* →列出所有名稱裡有 object 的主題– Get-Help about* →列出所有概念主題– Get-Help about_redirection →列出『重新導向』概

念主題的說明內容

-14-

Page 15: 0220 Windows server 2008 PowerShell

Get-Help ( 續 )

• 有些主題或 cmdlet 還可以利用 Get-Help/Help 的 -detailed 或 -full 參數,列出包含範例的詳細或所有說明,而 -examples 參數則可以列出範例;例如:– Get-Help Get-Help -detailed →列出 Get-Help 的詳細說明– Help dir -full →列出 dir 的完整說明– Help dir -examples →僅列出 dir 的範例

-15-

Page 16: 0220 Windows server 2008 PowerShell

指令的參數與用法• 除了 cmdlet , Windows PowerShell 的指令還包括別名、函式、程式、 script 檔案

• 輸入指令的同時,有時候必須輸入適當的參數。不同類型的指令,其參數的格式亦略有差異。

• 對 Cmdlet 及別名來說,有些參數包含成對的參數名稱及參數值,有些參數則只有參數名稱或參數值,而參數名稱是以短橫線作為前置符號;例如以下的 -name dir 就是成對的參數名稱 (-name) 和參數值 (dir) , -examples 則沒有參數值:– Get-Help -name dir -examples

-16-

Page 17: 0220 Windows server 2008 PowerShell

指令的參數與用法 ( 續 )

• 指令的參數分成必要參數和可以省略的選用參數兩類,查詢指令用法的說明會以一對半形的方括號括住選用參數,必要參數則不會有符號括住;以方括號括住表示可以省略。例如 Get-Help 的用法:– Get-Help [[-name] <string>] [-component

<string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-examples] [<CommonParameters>]

• 查詢指令的用法時,經常會看到以上用法說明,其中:

-17-

Page 18: 0220 Windows server 2008 PowerShell

指令的參數與用法 ( 續 )

• 指令的參數分成必要參數和可以省略的選用參數兩類,查詢指令用法的說明會以一對半形的方括號括住選用參數,必要參數則不會有符號括住;以方括號括住表示可以省略。例如 Get-Help 的用法:– Get-Help [[-name] <string>] [-component

<string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-examples] [<CommonParameters>]

-18-

Page 19: 0220 Windows server 2008 PowerShell

指令的參數與用法 ( 續 )

• 查詢指令的用法時,經常會看到以上用法說明,其中:– 一共有 7 個選用參數,並沒有必要參數。 – 前 5 個參數包含了成對的參數名稱和參數值。 – 前 5 個參數包含了成對的參數名稱和參數值。 – [<CommonParameters>] 參數只有參數值;這個參數意指每

個 cmdlet 都支援的共同參數。 – 第 1 個參數的參數名稱也以方括號括住,表示這個參數名稱可

以省略,所以我們可以只輸入 Get-Help dir 。 – •第 1 個參數的參數值是 <string> ,表示這個參數值為字串;以角括號括住,表示必須輸入適當的參數值,而不是輸入 string 。

-19-

Page 20: 0220 Windows server 2008 PowerShell

指令的參數與用法 ( 續 )

• 以實例來說明參數名稱的簡略輸入法則,例如以下的指令:– Get-Help -name dir –examples 上述指令經過 cmdlet 的參數名稱簡略輸入法則可以變成:

– Get-Help -n dir –ex 或– Get-Help -nam dir -exa

-20-

Page 21: 0220 Windows server 2008 PowerShell

Object Pipelines in Actiongps|where{$_.handles -gt 500}|sort handles|ft gps|where{$_.handles -gt 500}|sort handles|ft

name,handlesname,handles

gps(Get-

Process)

Operating system platform

Windows PowerShell engineWindows PowerShell engine

where(Where-Object)

sort(Sort-Object)

ft(Format-Table)

(Out-Host)

gps | where{$_.handles -gt 500} | sort handles | ft name,handles

Page 22: 0220 Windows server 2008 PowerShell

演練

-22-

Page 23: 0220 Windows server 2008 PowerShell

結論

Page 24: 0220 Windows server 2008 PowerShell

Community Resources• Newsgroup

Microsoft.Public.Windows.PowerShell

• Channel 9 taghttp://channel9.msdn.com/tags/Monad

• Team bloghttp://blogs.msdn.com/PowerShell/

• Wikihttp://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellWiki

• Script Centerwww.microsoft.com/technet/scriptcenter/hubs/msh.mspx

• Lots of community blogs (e.g.)www.leeholmes.com/blogwww.thepowershellguy.com

• Windows PowerShell Analyzer toolwww.karlprosser.com/coder/?cat=8

Page 25: 0220 Windows server 2008 PowerShell

For More Information

• Visit TechNet at:www.microsoft.com/technet

• Windows PowerShell: www.microsoft.com/powershellwww.microsoft.com/windowsserver/longhorn/powershell.mspx

• Books:– Windows PowerShell in Action by Bruce Payette (Manning Publications, 2007)

• http://manning.com/powershell/ – Microsoft Windows PowerShell: TFM by Don Jones and Jeffery Hicks (SAPIEN

Press, 2007)• www.sapienpress.com/

– Windows PowerShell Step by Step by Ed Wilson (Microsoft Press®, 2007)• www.microsoft.com/MSPress/books/10329.aspx

– Monad (AKA PowerShell): Introducing the MSH Command Shell and Language by Andy Oakley (O'Reilly Media, Inc., 2005)

• www.oreilly.com/catalog/msh/

Page 26: 0220 Windows server 2008 PowerShell

-26-

© 2007 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.