いつもお世話になっております。ぴろとでございます。本日の記事ですが、PowerShell リモーティング 機能は、WinRM を用いてリモートコンピュータを操作できる機能を ILM/FIM の拡張ルールに実装してしまうという内容となります。主な用途としては、Exchange 2010 のコマンドレットを投入するといった部分になるかと思いますが、同期シナリオによっては必要な場面も出てくるかと思います。今回のサンプルでは、コンソールアプリケーションとなりますが C# で実装しますので、あまり変更を加えなくても、拡張ルールに反映することが可能となります!
まずは、プログラムを作成するための前準備から行いましょう。
1) Windows SDK for Windows 7 をインストール ここについては、Tech Fielders のご紹介ページがよくできているので、こちらをご覧いただけますと幸いです。 http://www.microsoft.com/japan/powerpro/TF/column/mo_08_1.mspx
2) Visual Studio 2008 を起動し、C# のコンソール アプリケーションを選択します。
3) ソリューション エクスプローラーから、[参照設定] を右クリックし、[参照の追加] を選択します。 [参照] タブを選択し、 [C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0] フォルダを選び、 [System.Management.Automation.dll] を選択します。
4) プログラムのコーディングを行います。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management.Automation; // Added Line using System.Management.Automation.Runspaces; // Added Line using System.Security; // Added Line namespace RemotePS { class Program { static void Main(string[] args) { PSCredential cred; Runspace rs; string password = @"Password"; SecureString str = new SecureString(); foreach (char ch in password) { str.AppendChar(ch); } // Create PSCredential instance cred = new PSCredential(@"jpdsfim1\Administrator", str); // Create Runspace string ShellUri = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; WSManConnectionInfo connectionInfo = new WSManConnectionInfo( new Uri(@"http://jpdsfim1-exch/powershell"), ShellUri, cred); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; connectionInfo.MaximumConnectionRedirectionCount = 3; rs = RunspaceFactory.CreateRunspace(connectionInfo); rs.Open(); // Create PSCommand instance PSCommand command = new PSCommand(); command.AddCommand("get-recipient"); command.AddParameter("Identity", "aaabaa"); // Invoke the command using (PowerShell shell = PowerShell.Create()) { shell.Runspace = rs; shell.Commands = command; foreach (PSObject result in shell.Invoke()) { Console.WriteLine(result.Members["name"].Value.ToString()); } PSDataCollection<ErrorRecord> error = shell.Streams.Error; if (error.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (ErrorRecord record in error) { sb.AppendLine("\n" + record.ToString()); } } } rs.Close(); } } }
本サンプルでは、http://jpdsfim1-exch/powershell に接続を行うようにハードコードされたものとなりますため、環境に合わせて変更いただく必要があります。また、ShellUri 変数には、”http://schemas.microsoft.com/powershell/Microsoft.Exchange” を指定することで、Exchange 2010 のコマンドレットが使用可能となります。今回の例では、get-recipient コマンドレットの投入を行い、メールボックスを探す処理になります。これを、Attributes Flow に設定することで、該当ユーザーがメールボックスを持っているかを確認し、メタバースに反映させるといった処理が可能になります!ぜひぜひ、お試しください!
ぴろと@I’m very awful, because I got a hole in my tire.