MMC マネージスナップインの作成
このデモの内容
ここでは、作成 (開発) した Windows PowerShell の管理機能をベースとして、 GUI を提供する MMC (Microsoft Management Console) 3.0 のカスタムのスナップインを作成する手順をご紹介します。
デモでご紹介しているソースコード
【カスタムのスナップインクラスの実装 (C#)】
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ManagementConsole;
using System.ComponentModel;
namespace Sample1
{
[SnapInSettings("{D3884407-170B-4f03-A973-B36272A248F9}",
DisplayName = "My Proc Viewer Demo",
Description = "this is demo to display process lists in MMC.")]
public class MySnapIn : SnapIn
{
public MySnapIn()
{
this.RootNode = new ScopeNode();
this.RootNode.DisplayName = "Process Viewer";
FormViewDescription fvd = new FormViewDescription();
fvd.ControlType = typeof(ProcControl);
fvd.ViewType = typeof(ProcView);
fvd.DisplayName = "プロセス一覧";
this.RootNode.ViewDescriptions.Add(fvd);
this.RootNode.ViewDescriptions.DefaultIndex = 0;
}
}
[RunInstaller(true)]
public class InstallUtilSupport : SnapInInstaller
{
}
}
【MMCのビュークラスの実装 (C#)】
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ManagementConsole;
using System.Management.Automation;
using System.Collections.ObjectModel;
using Microsoft.ManagementConsole.Advanced;
namespace Sample1
{
class ProcView : FormView
{
protected override void OnInitialize(AsyncStatus status)
{
base.OnInitialize(status);
RunspaceInvoke invoker = new RunspaceInvoke();
Collection<PSObject> processes = invoker.Invoke("Get-Process");
ProcControl procControl = (ProcControl)this.Control;
procControl.RefreshData(processes);
}
protected override void OnSelectionAction(Action action, AsyncStatus status)
{
switch ((string)action.Tag)
{
case "KillProcess":
MessageBoxParameters msgParm = new MessageBoxParameters();
msgParm.Caption = "テスト";
msgParm.Buttons = System.Windows.Forms.MessageBoxButtons.OK;
msgParm.Text = "テストです";
this.SnapIn.Console.ShowDialog(msgParm);
System.Threading.Thread.Sleep(1000); // Do Work !
break;
}
}
}
}
【WindowsフォームによるMMCのコントロールクラスの実装 (C#)】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.ManagementConsole;
using System.Management.Automation;
namespace Sample1
{
public partial class ProcControl : UserControl, IFormViewControl
{
public ProcControl()
{
InitializeComponent();
}
#region IFormViewControl メンバ
private ProcView procView;
public void Initialize(FormView view)
{
procView = (ProcView)view;
procView.SelectionData.ActionsPaneItems.Clear();
procView.SelectionData.ActionsPaneItems.Add(
new Action("Kill Process", "kill the process", -1, "KillProcess"));
}
#endregion
internal void RefreshData(
System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> processes)
{
foreach (PSObject psobj in processes)
{
System.Diagnostics.Process myProcess = (System.Diagnostics.Process)psobj.BaseObject;
ListViewItem lvi = new ListViewItem();
lvi.Text = myProcess.Id.ToString();
lvi.SubItems.Add(myProcess.ProcessName);
listView1.Items.Add(lvi);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
procView.SelectionData.Clear();
}
else
{
textBox1.Text = listView1.SelectedItems[0].Text;
textBox2.Text = listView1.SelectedItems[0].SubItems[1].Text;
procView.SelectionData.Update(listView1.SelectedItems[0].Text, false, null, null);
procView.SelectionData.DisplayName = listView1.SelectedItems[0].Text;
}
}
}
}
ページのトップへ