开发者

Delphi Wmi Query on a Remote Machine

开发者 https://www.devze.com 2023-02-10 03:26 出处:网络
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.

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 w开发者_开发问答ith 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.

  • Securing a Remote WMI Connection
  • Connecting to WMI Remotely Starting with Windows Vista
  • Troubleshooting Error Code 80070005 - Access Denied (great page to find solution about connections problems)

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.
0

精彩评论

暂无评论...
验证码 换一张
取 消