uses WbemScripting_TLB;
function remoteExecute(programName:
string; machine:string='
'; user:string='
'; password:string='
'):
string;
var
SWbemLocator1:TSWbemLocator;
Service: ISWbemServices;
InParam, OutParam, SObject: ISWbemObject;
Method: ISWbemMethod;
SProp1, SProp2, MyProperty: ISWbemProperty;
s,methodName:
string;
PropValue:OleVariant;
begin
methodName:='
Create';
// CoInitialize(nil);
SWbemLocator1:=TSWbemLocator.Create(
nil);
if machine='
'
then
machine:='
.';
Service:=SWbemLocator1.ConnectServer(machine,'
root\CIMV2', user, password, '
','
', 0,
nil);
Service.Security_.Set_ImpersonationLevel(wbemImpersonationLevelImpersonate);
SObject:= Service.Get('
Win32_Process', 0,
nil);
Method:= SOBject.Methods_.Item(methodName, 0);
InParam:= Method.InParameters.SpawnInstance_(0);
MyProperty:= InParam.Properties_.Add('
CommandLine', wbemCimtypeString, False, 0);
PropValue:= programName;
MyProperty.Set_Value(PropValue);
MyProperty:= InParam.Properties_.Add('
CurrentDirectory', wbemCimtypeString, False, 0);
PropValue:= Null;
MyProperty.Set_Value(PropValue);
MyProperty:= InParam.Properties_.Add('
ProcessStartupInformation', wbemCimtypeObject, False, 0);
PropValue:= Null;
MyProperty.Set_Value(PropValue);
OutParam:= SObject.ExecMethod_(methodName, InParam, 0,
nil);
// OutParam:= SObject.ExecMethod_(methodName, nil, 0, nil);
SProp1:= outParam.Properties_.Item('
ReturnValue', 0);
SProp2:= outParam.Properties_.Item('
ProcessId', 0);
case SProp1.Get_Value
of
0:s:='
Successful completion.';
2:s:='
Access denied.';
3:s:='
Insufficient privilege.';
8:s:='
Unknown failure.';
9:s:='
Path not found.';
21:s:='
Invalid parameter.';
else s:='
Unknown reply code!';
end;
SWbemLocator1.Free;
service:=nil;
SObject:=nil;
OutParam:=nil;
SProp1:=nil;
result:=s+'
(PID='+inttostr(SProp2.Get_Value)+'
)';
// CoUninitialize;
end;