Thema: Delphi WMI Ping, aber wie?

Einzelnen Beitrag anzeigen

ddcool

Registriert seit: 8. Jun 2003
146 Beiträge
 
Delphi XE2 Architect
 
#3

Re: WMI Ping, aber wie?

  Alt 10. Mai 2007, 07:52
Hier der Code aus VBS:

Code:
Dim sHost      'name of Windows XP computer from which the PING command will be initiated
Dim sTarget      'name or IP address of remote computer to which connectivity will be tested
Dim cPingResults   'collection of instances of Win32_PingStatus class
Dim oPingResult      'single instance of Win32_PingStatus class

sHost      = "SWYNKPC-XP001"
sTarget      = "192.168.12.14"

Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
      sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
      "WHERE Address = '" + sTarget + "'")

For Each oPingResult In cPingResults
   If oPingResult.StatusCode = 0 Then
      If LCase(sTarget) = oPingResult.ProtocolAddress Then
         WScript.Echo sTarget & " is responding"
      Else
         WScript.Echo sTarget & "(" & oPingResult.ProtocolAddress & ") is responding"
      End If
      Wscript.Echo "Bytes = " & vbTab & oPingResult.BufferSize
      Wscript.Echo "Time (ms) = " & vbTab & oPingResult.ResponseTime
      Wscript.Echo "TTL (s) = " & vbTab & oPingResult.ResponseTimeToLive
   Else
      WScript.Echo sTarget & " is not responding"
      WScript.Echo "Status code is " & oPingResult.StatusCode
   End If
Next

Und hier mein eigener Code:

Delphi-Quellcode:
function getWMIValue(WMI_SÉRVER:string;WMI_CLASS:string;WMI_VALUE:string): TStringList;
var
   l_WMILocator: ISWbemLocator;
   l_WMIServices: ISWbemServices;
   l_WMIObjectDefinition: ISWbemObject;
   l_WMIObjectSet: SWbemObjectSet;
   l_WMIObjectInstances: IEnumVariant;
   l_WMIObject: ISWbemObject;
   l_WMIPropertySet: ISWbemPropertySet;
   l_WMIProperty: ISWbemProperty;

   l_TempObj: OleVariant;
   l_ObjValue: Cardinal;
   
begin
   result := TStringList.Create;

    try
      l_WMILocator := CoSWbemLocator.Create;
      l_WMIServices := L_WMILocator.ConnectServer(WMI_SÉRVER, 'root\CIMV2', '', '', '', '', 0, nil);
      l_WMIObjectDefinition := l_WMIServices.Get(WMI_CLASS, wbemFlagUseAmendedQualifiers, nil);
      l_WMIObjectSet := l_WMIObjectDefinition.Instances_(0, nil);
      l_WMIObjectInstances := (l_WMIObjectSet._NewEnum) as IEnumVariant;
    except
      result.Add('');
      Exit;
    end;

   while (l_WMIObjectInstances.Next(1, l_TempObj, l_ObjValue) = S_OK) do
   begin
      l_WMIObject:= IUnknown(l_TempObj) as SWBemObject;

      l_WMIPropertySet := l_WMIObject.Properties_;
      l_WMIProperty := l_WMIPropertySet.Item(WMI_VALUE, 0);

      if not VarIsNull(l_WMIProperty.Get_Value) then
        result.Add(l_WMIProperty.Get_Value) else result.Add('');
   end;
end;


procedure TfrmMain.ButtonClick(Sender: TObject);
begin
   if getWMIValue('Testrechner','Win32_PingStatus','StatusCode').Strings[0] = '0then ShowMessage('Es hat geklappt')
      else ShowMessage('Bäähh du darfst nicht!');
end;
Achso es hat nichts damit zu tun dass der Statuscode ein Integer im WMI ist. Andere Integer lassen sich mit dieser Funktion auch Problemlos auslesen.


Grüße

ddcool
  Mit Zitat antworten Zitat