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.