1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| unit Main;
{ Reboot/arrête un PC distant}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,WbemScripting_TLB, ActiveX;
type
TForm1 = class(TForm)
BtConnexion: TButton;
procedure BtConnexionClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
WMILocator: TSWbemLocator;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses ComObj,OleServer;
procedure TForm1.BtConnexionClick(Sender: TObject);
var
WmiService: SWbemServices;
WmiObjet: SWbemObject;
wmiObjetSet: SWbemObjectSet;
WmiProperty: SWbemProperty;
ObjectEnumerator: IEnumVariant;
ArrayVariant: OleVariant; // Tableau de variant
NumberItem: LongWord;
ShutdownMethod_inParameters,
Shutdown : SWbemObject;
PropertyReboot : OleVariant;
ShutdownMethod: SWbemMethod;
begin
WMILocator:= TSWbemLocator.Create(self);
{-----------------------------------------------------------------------------}
// Initialisation
try
{By default, all members of the local users group on Windows NT/Windows 2000
systems have read-only access to all namespaces. To change the default access
permissions from Windows NT, use the WMI Control application (Wbemcntl.exe).
To change the default access permissions from Windows 2000, use the
Microsoft® Management Console.
}
// Sécurité par défaut ( cf la base de registre ). Peut être omis
WMILocator.Security_.Set_ImpersonationLevel(wbemImpersonationLevelImpersonate );
WMILocator.Security_.Set_AuthenticationLevel(wbemAuthenticationLevelDefault);
{ The WbemImpersonationLevelEnum constants define the security impersonation levels :
1 wbemImpersonationLevelAnonymous
Short name: Anonymous
Hides the credentials of the caller. Calls to WMI may fail with this impersonation level.
2 wbemImpersonationLevelIdentify
Short name: Identify
Allows objects to query the credentials of the caller. Calls to WMI may fail with this impersonation level.
3 wbemImpersonationLevelImpersonate
Short name: Impersonate
Allows objects to use the credentials of the caller. This is the recommended impersonation level for Scripting API for WMI calls.
4 wbemImpersonationLevelDelegate
Short name: Delegate
Windows 2000 and later: Allows objects to permit other objects to use the credentials of the caller. This impersonation will work with Scripting API for WMI calls but may constitute an unnecessary security risk.
}
{The WbemAuthenticationLevelEnum constants define the security authentication levels :
0 WbemAuthenticationLevelDefault
Short name: Default
WMI uses the default Windows Authentication setting.
1 WbemAuthenticationLevelNone
Short name: None
Uses no authentication.
2 WbemAuthenticationLevelConnect
Short name: Connect
Authenticates the credentials of the client only when the client establishes a relationship with the server.
3 WbemAuthenticationLevelCall
Short name: Call
Authenticates only at the beginning of each call when the server receives the request.
4 WbemAuthenticationLevelPkt
Short name: Pkt
Authenticates that all data received is from the expected client.
5 WbemAuthenticationLevelPktIntegrity
Short name: PktIntegrity
Authenticates and verifies that none of the data transferred between client and server has been modified.
6 WbemAuthenticationLevelPktPrivacy
Short name: PktPrivacy
Authenticates all previous impersonation levels and encrypts the argument value of each remote procedure call.
}
// Attention aux majuscules-minuscules pour le password.
WmiService:= WMILocator.ConnectServer('P600', 'ROOT\CIMV2', 'MyDomaine\Administrateur', 'lolo', '',
'', wbemConnectFlagUseMaxWait, nil);
except
on E:EOleException do
begin
// E_ACCESSDENIED=$80070005 si pb de mot de passe
ShowMessage(StrOriginError(E.ErrorCode)+' ['+IntToHex(E.ErrorCode,8)+'] '+E.Message);
Raise;
end;
end;
//Ajoute au process appelant le privilége wbemPrivilegeShutdown (SE_SHUTDOWN_NAME)
wmiService.Security_.Privileges.Add(wbemPrivilegeShutdown, True);
wmiObjetSet := wmiService.ExecQuery(
'SELECT * FROM Win32_OperatingSystem WHERE Primary=True',
'WQL', wbemFlagReturnImmediately, nil);
// Affecte un énumérateur pour la collection d'objet SWbemObject
ObjectEnumerator:= (WmiObjetSet._NewEnum) as IEnumVariant;
// Retourne NumberItem éléments dans le tableau ArrayVariant,
// ici 1 élément est demandé
while (ObjectEnumerator.Next(1, ArrayVariant, NumberItem) = S_OK) do
begin
// Récupére de la collection l'objet SWbemObject courant
WmiObjet := IUnknown(ArrayVariant) as SWBemObject;
ShutdownMethod:= wmiObjet.Methods_.Item('Win32Shutdown', 0);
ShutdownMethod_inParameters:= ShutdownMethod.InParameters;
Shutdown:= ShutdownMethod_inParameters.SpawnInstance_(0);
WmiProperty := Shutdown.Properties_.Add('Flags', wbemCimtypeSint32, False, 0);
PropertyReboot:= EWX_SHUTDOWN; // EWX_REBOOT;
{Value Meaning
0 Log Off
0 + 4 Forced Log Off
1 Shutdown
1 + 4 Forced Shutdown
2 Reboot
2 + 4 Forced Reboot
8 Power Off
8 + 4 Forced Power Off
}
wmiProperty.Set_Value(PropertyReboot);
wmiObjet.ExecMethod_('Win32Shutdown', Shutdown, 0, nil);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
WMILocator.Free;
end;
end. |
Partager