どっとねっとふぁん

C# Tips And Samples
in 検索

24.WMIを使ってリモートPCを管理する

最新の投稿は、投稿日時: 06-22-2005, 4:23 午後 投稿者: ono です。スレッドには 0 件の返答があります。
投稿のソート: 前へ 次へ
  •  06-22-2005, 4:23 午後 33

    24.WMIを使ってリモートPCを管理する

    WMIを利用するとローカルのPCだけでなくリモートのPCも管理することが可能です。
    まぁ、WMIにどんなクラスがあって、どんなことができるのか、を探るのがなかなか大変だったりしますが。
    まずは、リモートPC上でのサービスの稼動状況をリストアップするサンプルです。

    -------------------------------------------------------
    using System;
    using System.Management;
    
    public class RemoteServiceList {
    
        public static void Main() {
    
            Console.Write("コンピュータ名:");
            string svr = Console.ReadLine();
            Console.Write("ユーザ名:");
            string usr = Console.ReadLine();
            Console.Write("パスワード:");
            string pass = Console.ReadLine();
    
            ConnectionOptions options = new ConnectionOptions();
            options.Username = usr;
            options.Password = pass;
    
            string mPath = @"\\" + svr + @"\root\cimv2";
            ManagementScope scope = new ManagementScope(mPath, options);
    
            try
            {
                scope.Connect();
                ManagementClass osClass = new ManagementClass( scope, new ManagementPath("Win32_Service"), null);
                ManagementObjectCollection m = osClass.GetInstances();
                foreach(ManagementObject mo in m)
                {
                    Console.Write(mo.Properties["State"].Value + " ");
                    Console.WriteLine(mo.Properties["DisplayName"].Value);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    
    -------------------------------------------------------

    コンピュータ名として自分自身を指定したような場合、ログオンしているユーザが実行権限のあるユーザだと入力したユーザ名/パスワードは無視されてしまうようです。

    もうひとつのサンプルは、リモートPCを再起動します。

    -------------------------------------------------------
    using System;
    using System.Management;
    
    class Sample_ManagementClass
    {
        public static void Main(string[] args) {
            Console.Write("コンピュータ名:");
            string svr = Console.ReadLine();
            Console.Write("ユーザ名:");
            string usr = Console.ReadLine();
            Console.Write("パスワード:");
            string pass = Console.ReadLine();
    
            ConnectionOptions options = new ConnectionOptions();
            options.Username = usr;
            options.Password = pass;
            options.EnablePrivileges = true;
    
            string mPath = @"\\" + svr + @"\root\cimv2";
            ManagementScope scope = new ManagementScope(mPath, options);
    
            try
            {
                scope.Connect();
                System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, oq);
                ManagementObjectCollection collection = searcher.Get();
                foreach( ManagementObject mo in collection )
                {
                    string[] ss={""};
                    mo.InvokeMethod("Reboot",ss);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    
    -------------------------------------------------------

    こちらのサンプルではローカルのPCを指定した場合、管理者権限を持ったユーザ名/パスワードを入力してもエラーが表示されました。
    ローカルマシンのリブートはちょっと考えないといけないようです。

    #管理者権限を持ったユーザでログオンしている場合だけを考えるなら簡単なんですが。

    なお、"Reboot"の部分を"Shutdown"に置き換えると、シャットダウンが行われます。


    あおい情報システム株式会社
     小野修司(どっとねっとふぁん)
RSS ニュースフィードで参照
SkinName:iroha_CS2
Powered by Community Server, by Telligent Systems