Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Prozesse Starten auf remote Server (https://www.delphipraxis.net/96429-prozesse-starten-auf-remote-server.html)

Centrii 24. Jul 2007 14:27


Prozesse Starten auf remote Server
 
Hallo zusammen

Ich hab da mal ne Frage....

Ich bin grad dabei über WMI Prozesse auf anderen PC´s zu starten. Beenden usw. ist kein Problem.
Mein bisheriger Ansatz sieht so aus....

Delphi-Quellcode:
procedure TfProzesse.WMIStartProcesses(Computer, User, Password,exeName: string);

  function GetCompName: string;
  var
    Buf       : array[0..MAX_COMPUTERNAME_LENGTH] of Char;
    Size      : DWORD;
  begin
    Size := SizeOf(Buf);
    if GetComputerName(Buf, Size) then
      Result := Buf
    else
      Result := '';
  end;

var
  FComputer: String;
  FUser: String;
  FPassword: String;
  Services    : ISWbemServices;
  ObjectDefinition: ISWbemObject;
  ObjectSet   : SWbemObjectSet;
  ObjectInstances: IEnumVariant;
  WMIObject   : ISWbemObject;
  PropertySet : ISWbemPropertySet;
  WMIProperty : ISWbemProperty;
  FProcess: ISWbemObject;
  FMethod: ISWbemMethod;
  FInParam: ISWbemObject;
  FOutParam: ISWbemObject;
  FProperty: ISWbemProperty;
  FPropValue: OleVariant;

  TempObj     : OleVariant;
  ObjValue    : Cardinal;
  i           : Integer;
resourcestring
  rsWMIError  = 'WMI-Fehler';
begin
  if AnsiUpperCase(GetCompName) = AnsiUpperCase(Computer) then
  begin
    FComputer := '';
    FUser := '';
    FPassword := '';
  end
  else
  begin
    FComputer := Computer;
    FUser := user;
    FPassword := Password;
  end;
  i := 0;
  //Locator := TSWbemLocator.Create(Nil);
  try
    try
      Services := Locator.ConnectServer(FComputer, 'root\cimv2',FUser, FPassword, '', '', 0, nil);
      if Services <> nil then
      begin
        Services.Security_.Set_ImpersonationLevel(wbemImpersonationLevelImpersonate);
        FProcess := Services.Get('Win32_Process', 0, nil);
        FMethod := FProcess.Methods_.Item('Create', 0);
        FInParam := FMethod.InParameters.SpawnInstance_(0);
        FProperty := FInParam.Properties_.Add('CommandLine', wbemCimtypeString, false, 0);
        FPropValue := exeName;
        FProperty.Set_Value(FPropValue);
        FOutParam := FProcess.ExecMethod_('Create', FInParam, 0, nil);
      end;
    finally
      Services := nil;
    end;
  except
    on e: Exception do
      raise e.Create(e.message);
  end;
end;
Jetzt hab ich das Problem das ich local auf meinem PC eine *.exe starten kann, auch das Form aufgeht und in der Prozessliste angezeigt wird. Mach ich das aber auf nem Remote PC wird der SourceCode der im "ONCreate" steht ausgeführt (als Beispiel Datei kopieren), die *.exe steht in der Prozessliste aber das Form geht nicht auf ;-)
Weiter würde ich gerne wissen was ich alles beim

Delphi-Quellcode:
FInParam.Properties_.Add('CommandLine', wbemCimtypeString, false, 0);
anstelle der ersten beiden Parametern eingeben kann.

Würde mich über eine Antwort sehr freuen :thumb:

Gruß

Ruben

Remko 24. Jul 2007 22:09

Re: Prozesse Starten auf remote Server
 
Your remote process runs in a restricted security context, it does not run in the context of an interactive logon account
(if any), it runs in the security context of the remote WMI service and by default it's impersonating the initiator (the WMI client). What does it mean to the application that was started? First, this application has no way to interact with the user, even when it's a console application, the output will go to a non visible desktop and there is no way to read
from the keyboard.

If you want to start a process remotely I'd suggest remotely installing a service, make that start your process and remove the service upon termination. You can use GetActiveConsoleSession (XP and higher) to obtain the interactive sessionid and then use WtsQueryUserToken to obtain the users token. Lastly use CreateProcessAsUser with the token from WtsQueryUserToken. You can put this in the ServiceStartEvent. Something like this:

Delphi-Quellcode:
procedure TService1.ServiceStart(Sender: TService; var Started: Boolean);
var hToken: THandle;
  si: _STARTUPINFOA;
  pi: _PROCESS_INFORMATION;
begin
  ZeroMemory(@si, SizeOf(si));
  si.cb := SizeOf(si);
  si.lpDesktop := nil;

  if WTSQueryUserToken(WTSGetActiveConsoleSessionId, hToken) then
  begin
    if CreateProcessAsUser(hToken, nil, 'cmd.exe', nil, nil, False,
      CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP, nil,
      nil, si, pi) then
    begin
      // Do some stuff
    end;
  end;
  Self.DoStop;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:53 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz