2

WmiSetを使用して、リモートマシンでWmiクエリを実行します。Delphi 2007では非常にうまく機能しますが、現在DelphiXEでは使用できません。

Wmi Queries以前のSOの質問から実行するコードを見つけました。DelphiでWmiを使用してください。で提供されているコードスニペットはAnswer No. 5、ローカルマシンで完全に機能しますが、リモートマシンでWmiクエリを実行できるかどうかを知る必要があります。

管理者の資格情報を使用してリモートマシンに接続しても、EOleSysError: Access is denied例外が発生します。

よろしく、ピーター。

4

1 に答える 1

8

ピーター。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.
于 2011-02-14T13:16:42.053 に答える