質問

We use WmiSet to do Wmi Queries on remote machines. It works very well in Delphi 2007 but is not currently available for Delphi XE.

I found some code to do Wmi Queries from a previous SO Question Use Wmi with Delphi. The code snippet provided in Answer No. 5 works perfectly well on my local machine, but I need to know if it is possible to execute the Wmi Query on a remote machine.

Even if I connect to the remote machine with my Administrator credentials, I get a EOleSysError: Access is denied exception.

Regards, Pieter.

役に立ちましたか?

解決

Pieter. Before to connect to a remote machine using the WMI you must enable the DCOM access to the specified user in the remote machine.

Read these articles to understand and fix problems connecting to remote machines using the WMI.

Additionally here i leave a more clear code to connect to the wmi in a remote machine. check the part where the EOleException exception is processed to get the error code and found the cause of the issue.

program WMIRemote;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure  GetWMIOSInfo(const RemoteMachine,User,Password : string);
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(RemoteMachine, 'root\CIMV2', User, Password);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_OperatingSystem','WQL',0);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
    while oEnum.Next(1, FWbemObject, iValue) = 0 do
    begin
        Writeln(FWbemObject.Name);
        //code
        FWbemObject:=Unassigned;
    end;
    FWbemObjectSet:=Unassigned;
end;

begin
 try
    CoInitialize(nil);
    try
      //GetWMIOSInfo('localhost','','');
      GetWMIOSInfo('192.168.52.2','Administrator','password');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('Error Code %d ($%x) Msg : %s',[E.ErrorCode,E.ErrorCode, E.Message]));

    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Readln;
end.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top