下記のサイトを参考にさせていただき、Powershell で ハッシュ値を計算します。

DOBON.NET MD5(SHA1等)ハッシュ値を計算する
http://dobon.net/vb/dotnet/string/md5.html

 

文字列のMD5ハッシュ値を計算する

#MD5ハッシュ値を計算する文字列
$s = "hogehoge";

#文字列をbyte型配列に変換する
$data = [System.Text.Encoding]::ASCII.GetBytes($s)

# MD5ハッシュ値を計算する
$md5 = [System.Security.Cryptography.MD5]::Create()
$hash = $md5.ComputeHash($data);
$result = [System.BitConverter]::ToString($hash).ToLower().Replace("-","")

Write-Output $result

 

Webサイトなどからダウンロードしたファイルのハッシュ値を調べたいときは、StreamReader を使用します。

ファイルのMD5ハッシュ値を計算する

$stream = New-Object IO.StreamReader "C:\Users\xxxx\Downloads\xxxxx.zip"

# MD5ハッシュ値を計算する
$md5 = [System.Security.Cryptography.MD5]::Create()
$hash = $md5.ComputeHash($stream.BaseStream);
$result = [System.BitConverter]::ToString($hash).ToLower().Replace("-","")

Write-Output $result

環境:Windows Server 2008 R2 (IIS7.5)

Windows Server 2008 R2 には標準でIIS の PowerShell プロバイダが入っていますので、 PowerShellから簡単にIISの管理を行うことができます。

今回は、IISマネージャーの「サイトの管理」で表示される「サイト名」「アプリケーションプール」「物理パス」を変更してみます。

 

管理者権限で Windows PowerShell を実行し、実行ポリシーの変更、IIS WebAdministration モジュールをインポートします。

Set-ExecutionPolicy RemoteSigned
import-module WebAdministration

 

サイトの編集は下記のコマンドで行うことができます。既存の値を変更しますので、「Set-ItemProperty」を使用します。

物理パスの変更

Set-ItemProperty "IIS:\Sites\Default Web Site" -name PhysicalPath -value "C:\inetpub\wwwroot2"

アプリケーションプールの変更

Set-ItemProperty "IIS:\Sites\Default Web Site" -name Applicationpool -value "Classic .NET AppPool"

サイト名の変更

Set-ItemProperty "IIS:\Sites\Default Web Site" -name name -value "New Web Site"

 

変更前(デフォルト)

変更前

変更後

02

PowerShellでパフォーマンス カウンタ オブジェクト(System.Diagnostics.PerformanceCounter)を使用して、パフォーマンス カウンタの値を取得します。

下記のコードではプロセッサの利用状況を1秒おきに取得しています。

$p = new-object System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total") 
[Void] $p.NextValue()

while($true){
    Start-Sleep -s 1
	Write-Output ([string]::Format("{0:n2} %", $p.NextValue()))
}

 

パフォーマンスカウンタなのでネットワークの使用状況や

$n = new-object System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Total/sec", "インスタンス名") 
[Void] $n.NextValue()
while($true){
    Start-Sleep -s 1
	Write-Output ([string]::Format("{0:n0} Bytes/Sec", $n.NextValue()))
}

 

メモリの空きなども取得できます。

$m = new-object System.Diagnostics.PerformanceCounter("Memory", "Available MBytes", "") 
[Void] $m.NextValue()
Write-Output ([string]::Format("{0} MB", $m.NextValue()))

環境:Windows 7 + IE8

PowerShell で InternetExplorer.Application(COMオブジェクト)の操作をします。

下記の例では、Internet Explorerを呼びだして、「http://www.yahoo.co.jp/」にアクセス後、テキストボックスに「Yahoo」と入力し「検索」ボタンをクリックしています。

$ie = new-object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("http://www.yahoo.co.jp/")

# 読み終わるまで待ち続ける
While($ie.Busy)
{
    Start-Sleep -milliseconds 100
}

$doc = $ie.document 
$txt = $doc.getElementByID("srchtxt")
$txt.value = "yahoo"

$btn = $doc.getElementByID("srchbtn")
$btn.click()

フィードを作成・配信する で Web フォーム を使って RSSを作成し配信したので、コードはほとんど同じですが、今回は ASP.NET MVC で行います。

サンプルコードは、SyndicationFeed を使用するため、System.ServiceModel.Web を参照に追加する必要があります。

using System;
using System.IO;
using System.Xml;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using System.Collections.Generic;

namespace Sample.Controllers
{
    public class FeedController : Controller
    {
        public ActionResult Rss()
        {
            SyndicationFeed feed = new SyndicationFeed();
            feed.Title = new TextSyndicationContent("RSS 作成・配信(Title)");
            feed.Description = new TextSyndicationContent("RSS 作成・配信のテストプログラムです(Description)");
            List<SyndicationItem> items = new List<SyndicationItem>();

            SyndicationItem item1 = new SyndicationItem();
            item1.Title = new TextSyndicationContent("仮想サーバーへの移行");
            item1.Content = SyndicationContent.CreatePlaintextContent("Virtual Serverで稼働していた仮想サーバーをHyper-Vで稼働させるために移行します。といっても、コピー程度ですが・・・");
            item1.PublishDate = DateTimeOffset.Now;
            SyndicationLink link1 = new SyndicationLink(new Uri("http://hogehoge"));
            item1.Links.Add(link1);
            items.Add(item1);

            feed.Items = items;
            var sw = new StringWriter();
            var xml = new XmlTextWriter(sw);
            feed.SaveAsRss20(xml);

            return Content(sw.ToString(), "application/rss+xml", System.Text.Encoding.UTF8);
        }
    }
}

※ Controller に直接書いています。