彼得。在使用 WMI 连接到远程计算机之前,您必须启用对远程计算机中指定用户的 DCOM 访问权限。
阅读这些文章以了解和解决使用 WMI 连接到远程计算机的问题。
另外在这里我留下了一个更清晰的代码来连接到远程机器中的 wmi。检查EOleException
异常处理的部分以获取错误代码并找到问题的原因。
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.