kkamegawa's weblog

Visual Studio,TFS,ALM,VSTS,DevOps関係のことについていろいろと書いていきます。Google Analyticsで解析を行っています

powershellをクラッシュさせるコード

Visual Studio 2008 Beta2日本語版が出たので、PowerShellで任意のURIをローカルに保存するスクリプト(関数)を書きました。

function DownloadInetFile
{
  if($args.length -ne 2) {
    [System.Console]::WriteLine("引数は url, localfilename を指定してください")
    return;
  }
  [Void][Reflection.Assembly]::LoadWithPartialName("System.Net")
  $a = new-object System.net.webclient
  $b = new-object system.uri $args[0]
  $a.add_DownloadFileCompleted({[System.Console]::WriteLine($b.AbsoluteUri + " ダウンロード終了")})
  $a.DownloadFileAsync($b, $args[1])
}

これで
PS>downloadinetfile http://www.exsamples.com/index.html c:\temp\exsamples.html
とかやるとPowerShellがPSInvalidOperationExceptionを出して、クラッシュします。どうもDownloadFileCompetedイベントに割りつけたスクリプトブロックがよくないようですね。staticなメソッド呼び出しをスクリプトブロックに書いちゃダメ?