2012-10-31 101 views
1

我使用mgmtclassgen.exe並獲取CIM_DataFile wmi類的包裝(DataFile.cs)類。下面的代碼在localhost上完美工作(沒有填充信息),但是當我連接到遠程機器變量returnResult = 9(無效對象)時。但是可變dataFileCollection的大小= 1wmi c#。從遠程機器複製文件

var _connectionOptions = new ConnectionOptions(); 
       _connectionOptions.Username = "username"; 
       _connectionOptions.Password = "password"; 
       _connectionOptions.Authority = String.Format("ntlmdomain:{0}", "DOMAIN"); 
var _managementScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", 
"RemotePCName"), _connectionOptions); 
    var dataFileCollection = DataFile.GetInstances(_managementScope, 
        @"Name = 'C:\\Windows\\System32\\mapisvc.inf'"; 
       var tempFilePath = "c:\\temp.txt"); 
       if (dataFileCollection.Count > 0) 
       { 
        foreach (var dataFile in dataFileCollection.Cast<DataFile>()) 
        { 
         var returnResult = dataFile.Copy(tempFilePath); 
         if (File.Exists(tempFilePath)) 
         { 
          List<string> lines = File.ReadAllLines(tempFilePath).ToList(); 
          File.Delete(tempFilePath); 
         } 
        } 
       } 

回答

2

嘗試不同的調整管理範圍 也許你可以試試這樣的:

ManagementObjectSearcher searcher = 
        new ManagementObjectSearcher(
         "\\\\" + strComputer + "\\root\\CIMV2", 
         "SELECT * FROM Win32_PerfFormattedData_MSSQLSERVER_SQLServerDatabases"); 

其中strComputer的是遠程PC和Win32_Perf的名字...的您正在嘗試查詢的課程。這適用於我,因爲它在本地網絡中,儘管我不確定遠程機器的位置。

你也可以去http://www.microsoft.com/en-us/download/details.aspx?id=8572這是微軟的WMI查詢生成器。這使您可以在C#,VB和VB腳本中生成查詢。設置連接屬性。 值得一試。

相關問題