Feb 16, 2007

[dotNet] Windows PowerShell にチャレンジ (1)

Windows PowerShell とは

Windows PowerShell は、Windows のコマンドライン機能を強化したものらしい。 要するに、cmd.exe の機能強化版ということだろう。 Windows PowerShell の公式サイトによると、

Microsoft Windows PowerShell command line shell and scripting language helps IT Professionals achieve greater productivity. Using a new admin-focused scripting language, more than 130 standard command line tools, and consistent syntax and utilities, Windows PowerShell allows IT Professionals to more easily control system administration and accelerate automation.
というものだそうだ。 未だにバッチファイルで何とか凌いでいた(何ともならない場合は Linux (^^;) ので、Windows PowerShell を少し勉強してみることに。
Windows PowerShell Web Site
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx
Windows PowerShell Blog
http://blogs.msdn.com/powershell/
Knowledge Base Article
http://support.microsoft.com/kb/926139
Windows PowerShell SDK
http://msdn2.microsoft.com/en-us/library/aa830112.aspx
@IT - 次世代Windowsシェル「Windows PowerShell」を試す
http://www.atmarkit.co.jp/fdotnet/special/powershell01/powershell01_01.html

Windows PowerShell のインストール

Windows PowerShell は最近 1.0 がリリースされたばかりの様だが、Windows Vista だけでなく Windows XP 版も用意されている。 素晴らしい。 とりあえず Windows Vista 環境が無いので、Windows XP Professional 環境に Windows PowerShell をインストールしてみた。 インストール手順は以下の通り。

  1. .Net Framework 2.0 をインストール。Microsoft .NET Framework Version 2.0 Redistributable Package (x86)からアーカイブをダウンロードしてインストール。
  2. Windows PowerShell 1.0 をインストール。Windows PowerShell 1.0 English-Language Installation Package for Windows XP (KB926139)からアーカイブをダウンロードする。
    1. Windows PowerShell のダウンロードをするためには Genuine Check をする必要がある。指示に従い GenuineCheck.exe をダウンロードして実行する。
    2. GenuineCheck で表示されるコードをダウンロードサイトに入力すると Windows PowerShell のアーカイブ WindowsXP-KB926139-x86-ENU.exe がダウンロードできる。
    3. WindowsXP-KB926139-x86-ENU.exe を実行し、後はインストーラの指示に従い Windows PowerShell をインストールする。
[インストール画面(1)] [インストール画面(2)] [インストール画面(3)] [インストール画面(4)]

Windows PowerShell の実行

Windows PowerShell のインストーラがスタートメニューにも登録してくれるが、一々面倒なので「ファイル名を指定して実行」から "powershell.exe" を実行しても良い。 一見 cmd.exe と変わらないウィンドウが表示されるが、コマンドプロンプトの前に "PS" が付いているので Windows PowerShell が実行されていることが分かる。
[Windows PowerShell 実行] [Windows PowerShell ウィンドウ]
とりあえず使用できるコマンド一覧を表示してみる。

PS C:\> Get-Command | sort

CommandType     Name                            Definition
-----------     ----                            ----------
Cmdlet          Add-Content                     Add-Content [-Path] <String[...
Cmdlet          Add-History                     Add-History [[-InputObject] ...
Cmdlet          Add-Member                      Add-Member [-MemberType] <PS...
Cmdlet          Add-PSSnapin                    Add-PSSnapin [-Name] <String...
Cmdlet          Clear-Content                   Clear-Content [-Path] <Strin...
Cmdlet          Clear-Item                      Clear-Item [-Path] <String[]...
Cmdlet          Clear-ItemProperty              Clear-ItemProperty [-Path] <...
Cmdlet          Clear-Variable                  Clear-Variable [-Name] <Stri...
Cmdlet          Compare-Object                  Compare-Object [-ReferenceOb...
Cmdlet          ConvertFrom-SecureString        ConvertFrom-SecureString [-S...
Cmdlet          Convert-Path                    Convert-Path [-Path] <String...
Cmdlet          ConvertTo-Html                  ConvertTo-Html [[-Property] ...
Cmdlet          ConvertTo-SecureString          ConvertTo-SecureString [-Str...
Cmdlet          Copy-Item                       Copy-Item [-Path] <String[]>...
Cmdlet          Copy-ItemProperty               Copy-ItemProperty [-Path] <S...
Cmdlet          Export-Alias                    Export-Alias [-Path] <String...
         (略)
ということで、.Net Framework が提供してくれる機能をかなり利用できることが分かる。