次の方法で共有


C を使用して WMI にリモート接続する#

PowerShell、VBScript、C++ などの他の言語と同様に、C# を使用してリモート コンピューター上のハードウェアとソフトウェアをリモートで監視できます。 マネージド コードのリモート接続は、Microsoft.Management.Infrastructure 名前空間を介して行われます。 (以前のバージョンの WMI では、System.Management 名前空間が使用されました。これは完全のためにここに含まれています)。

手記

System.Management は、WMI にアクセスするために使用された元の .NET 名前空間でした。ただし、この名前空間の API は一般に低速であり、Microsoft.Management.Infrastructure 対応する最新 に比べてスケーリングも行われません。

 

Microsoft.Management.Infrastructure 名前空間のクラスを使用してリモートで接続すると、基になるリモート メカニズムとして DCOM が使用されます。 WMI リモート接続は、偽装と認証の DCOM セキュリティ要件に準拠している必要があります。 既定では、スコープはローカル コンピューターと "Root\CIMv2" システム名前空間にバインドされます。 ただし、アクセスするコンピューター、ドメイン、WMI 名前空間の両方を変更できます。 権限、偽装、資格情報、その他の接続オプションを設定することもできます。

C# (Microsoft.Management.Infrastructure) を使用して WMI にリモート接続するには

  1. CimSession.Createを呼び出して、リモート コンピューター上にセッションを作成します。

    ログオンしているのと同じ資格情報 (ドメインとユーザー名) を使用してリモート コンピューターに接続する場合は、Create 呼び出しでコンピューターの名前を指定できます。 CimSession オブジェクト 返されたら、WMI クエリを作成できます。

    using Microsoft.Management.Infrastructure;
    ...
    string Namespace = @"root\cimv2";
    string OSQuery = "SELECT * FROM Win32_OperatingSystem";
    CimSession mySession = CimSession.Create("Computer_B");
    IEnumerable<CimInstance> queryInstance = mySession.QueryInstances(Namespace, "WQL", OSQuery);
    

    C# Microsoft.Management.Infrastructure API を使用して WMI クエリを実行する方法の詳細については、「WMI クラスまたはインスタンス データの取得」を参照してください。

  2. 異なる資格情報、ロケール、偽装レベルなど、接続に異なるオプションを設定する場合は、CimSession.Createの呼び出しで CimSessionOptions オブジェクトを使用する必要があります。

    CimSessionOptions は、WSManSessionOptions および DComSessionOptionsの基本クラスです。 WS-Man セッションと DCOM セッションでそれぞれオプションを設定する場合に使用できます。 次のコード サンプルでは、DComSessionOptions オブジェクトを使用して、偽装レベルを Impersonate に設定する方法について説明します。

    string computer = "Computer_B"
    DComSessionOptions DComOptions = new DComSessionOptions();
    DComOptions.Impersonation = ImpersonationType.Impersonate;
    
    CimSession Session = CimSession.Create(computer, DComOptions);
    
  3. 接続の資格情報を設定する場合は、CimCredentials オブジェクトを作成し、CimSessionOptionsに追加する必要があります。

    次のコード サンプルでは、WSManSessionOptions クラスを作成し、適切な CimSessionOptionsで埋め込み、CimSession.Create 呼び出しで使用する方法について説明します。

    string computer = “Computer_B”;
    string ___domain = “Domain1″;
    string username = “User1″;
    
    string plaintextpassword; 
    
    //Retrieve password from the user. 
    //For the complete code, see the sample at the bottom of this topic.
    
    CimCredential Credentials = new CimCredential(PasswordAuthenticationMechanism.Default, ___domain, username, securepassword); 
    
    WSManSessionOptions SessionOptions = new WSManSessionOptions();
    SessionOptions.AddDestinationCredentials(Credentials); 
    
    CimSession Session = CimSession.Create(computer, SessionOptions);
    

    一般に、アプリケーションにパスワードをハードコーディングしないことをお勧めします。上記のコード サンプルが示すように、可能な限りユーザーにパスワードのクエリを実行し、安全に保存してください。

WMI は、リモート コンピューター上のハードウェアとソフトウェアを監視することを目的としています。 WMI v1 のリモート接続は、ManagementScope オブジェクトを介して行われます。

C# (System.Management) を使用して WMI にリモート接続するには

  1. コンピューターの名前と WMI パスを使用して、ManagementScope オブジェクトを作成し、ManagementScope.Connect() を呼び出してターゲットに接続します。

    ログオンしているのと同じ資格情報 (ドメインとユーザー名) を使用してリモート コンピューターに接続する場合は、WMI パスを指定するだけで済みます。 接続したら、WMI クエリを作成できます。

    using System.Management;
    ...
    ManagementScope scope = new ManagementScope("\\\\Computer_B\\root\\cimv2");
    scope.Connect();
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    

    C# で System.Management API を使用して WMI クエリを実行する方法の詳細については、「WMI クラスまたはインスタンス データの取得」を参照してください。

  2. 別のドメイン内のリモート コンピューターに接続する場合、または別のユーザー名とパスワードを使用する場合は、ManagementScopeの呼び出しで ConnectionOptions オブジェクトを使用する必要があります。

    ConnectionOptions には、認証、偽装、ユーザー名、パスワード、およびその他の接続オプションを記述するためのプロパティが含まれています。 次のコード サンプルでは、ConnectionOptions を使用して、偽装レベルを Impersonate に設定する方法について説明します。

    ConnectionOptions options = new ConnectionOptions();
    options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
    
    ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\cimv2", options);
    scope.Connect();
    
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query);
    

    一般に、明示的に必要でない限り、偽装レベルを Impersonate に設定することをお勧めします。 さらに、名前とパスワードを C# コードに書き込まないようにしてください。 (可能であれば、実行時に動的に提供するようにユーザーにクエリを実行できるかどうかを確認してください)。

    リモート WMI 接続でさまざまなプロパティを設定する例については、「ConnectionOptions リファレンス ページ」の「例」セクションを参照してください。

Microsoft.Management.Infrastructure の例

次の C# コード例は、TechNet に関する次のブログ投稿に基づいて、CimCredentials を使用し、WSManSessionOptions して、リモート接続に資格情報を設定する方法について説明しています。

using System;
using System.Text;
using System.Threading;
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;
using System.Security; 

namespace SMAPIQuery
{
    class Program
    {
        static void Main(string[] args)
        { 

            string computer = "Computer_B";
            string ___domain = "DOMAIN";
            string username = "AdminUserName";


            string plaintextpassword; 

            Console.WriteLine("Enter password:");
            plaintextpassword = Console.ReadLine(); 

            SecureString securepassword = new SecureString();
            foreach (char c in plaintextpassword)
            {
                securepassword.AppendChar(c);
            } 

            // create Credentials
            CimCredential Credentials = new CimCredential(PasswordAuthenticationMechanism.Default, 
                                                          ___domain, 
                                                          username, 
                                                          securepassword); 

            // create SessionOptions using Credentials
            WSManSessionOptions SessionOptions = new WSManSessionOptions();
            SessionOptions.AddDestinationCredentials(Credentials); 

            // create Session using computer, SessionOptions
            CimSession Session = CimSession.Create(computer, SessionOptions); 

            var allVolumes = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_Volume");
            var allPDisks = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_DiskDrive"); 

            // Loop through all volumes
            foreach (CimInstance oneVolume in allVolumes)
            {
                // Show volume information

                if (oneVolume.CimInstanceProperties["DriveLetter"].ToString()[0] > ' '  )
                {
                    Console.WriteLine("Volume ‘{0}’ has {1} bytes total, {2} bytes available", 
                                      oneVolume.CimInstanceProperties["DriveLetter"], 
                                      oneVolume.CimInstanceProperties["Size"], 
                                      oneVolume.CimInstanceProperties["SizeRemaining"]);
                }

            } 

            // Loop through all physical disks
            foreach (CimInstance onePDisk in allPDisks)
            {
                // Show physical disk information
                Console.WriteLine("Disk {0} is model {1}, serial number {2}", 
                                  onePDisk.CimInstanceProperties["DeviceId"], 
                                  onePDisk.CimInstanceProperties["Model"].ToString().TrimEnd(), 
                                  onePDisk.CimInstanceProperties["SerialNumber"]);
            } 

            Console.ReadLine();
         }
     }
 }

System.Management の例

次の C# コード サンプルでは、System.Management オブジェクトを使用した一般的なリモート接続について説明します。

using System;
using System.Management;
public class RemoteConnect 
{
    public static void Main() 
    {
        ConnectionOptions options = new ConnectionOptions();
        options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

        
        ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\cimv2", options);
        scope.Connect();

        //Query system for Operating System information
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query);

        ManagementObjectCollection queryCollection = searcher.Get();
        foreach ( ManagementObject m in queryCollection)
        {
            // Display the remote computer information
            Console.WriteLine("Computer Name     : {0}", m["csname"]);
            Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
            Console.WriteLine("Operating System  : {0}", m["Caption"]);
            Console.WriteLine("Version           : {0}", m["Version"]);
            Console.WriteLine("Manufacturer      : {0}", m["Manufacturer"]);
        }
    }
}

リモート コンピューターでの WMI への接続