- 先建立連線 Remote 連線
//建立遠端連線 ConnectionOptions remoteCon = new ConnectionOptions(); remoteCon.Username =Account; remoteCon.Password = Password; //如果為NULL,則已電腦的網域為預設值 //remoteCon.Authority = "ntdlmdomain:DOMAIN"; //取得或設定 COM 驗證等級,以使用於這個連接中的作業。 remoteCon.Authentication = AuthenticationLevel.PacketPrivacy; //指出是否需要為連接作業啟用使用者權限。在執行的作業需要啟用某些使用者權限時 (例如,電腦重新開機) 才應該使用這個屬性。 remoteCon.EnablePrivileges = true;
- 設定Scope
scopePath = "\\\\" + IP + "\\root\\default";
private ManagementClass RegGetValue(string path, ConnectionOptions remoteCon,string ip)
{
//設定管理的範圍
ManagementScope remoteSop = new ManagementScope(path, remoteCon);
try
{
remoteSop.Connect();
}
catch (Exception e)
Export.LogWrt(string.Format("連線到{0}發生錯誤,錯誤訊息:{1}", ip, e.Message));
}
if (remoteSop.IsConnected)
{
ManagementClass methodCall = new ManagementClass(remoteSop, new ManagementPath("StdRegProv"), null);
return methodCall;
}
else
{
return null;
}
}
- 取得Method裡的參數
//取得method裡的參數instance
inParams = methodCall.GetMethodParameters("GetDWORDValue");
- 設定參數
inParams["hDefKey"] = 0x80000002; //HKEY_LOCAL_MACHINE inParams["sSubKeyName"] = @"SOFTWARE\Altiris\Altiris Agent\Software Delivery";
- 執行Method並取回Return Value
//執行Method並且取得Return Value
outParams = methodCall.InvokeMethod("GetDWORDValue", inParams, null);
if (Convert.ToInt32(outParams[i]["returnValue"]) == 0) //OK
{
tableList.Add(string.Format("{0:N2}", outParams["uValue"]));
}
else
{
tableList.Add(null);
}
參考︰
沒有留言:
張貼留言