Je crois que le titre dit tout.
Je souhaite lister tous les serveurs SQL Serveur qui tournent dans un domaine.
Je crois que le titre dit tout.
Je souhaite lister tous les serveurs SQL Serveur qui tournent dans un domaine.
Salut Laurent,
Voici du code qui devrait pouvoir t'aider.
Il s'agit d'un code qui liste toutes les machines du domaine et pour chaque machine, les services qui tournent...
Dans un module:
Sur un form: un array de 3 boutons(Command1(0), Command1(1) and Command1(2)), une list box (List1), une combo (Combo1) et un label (Label1).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 Option Explicit Public Type SERVICE_STATUS dwServiceType As Long dwCurrentState As Long dwControlsAccepted As Long dwWin32ExitCode As Long dwServiceSpecificExitCode As Long dwCheckPoint As Long dwWaitHint As Long End Type Public Type ENUM_SERVICE_STATUS lpServiceName As Long lpDisplayName As Long ServiceStatus As SERVICE_STATUS End Type 'our own constant Public Const SIZEOF_SERVICE_STATUS As Long = 36 'windows constants Public Const LB_SETTABSTOPS As Long = &H192 Public Const ERROR_MORE_DATA = 234 Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4 'Service State for Enum Requests (Bit Mask) Public Const SERVICE_ACTIVE = &H1 Public Const SERVICE_INACTIVE = &H2 Public Const SERVICE_STATE_ALL = SERVICE_ACTIVE Or SERVICE_INACTIVE 'Service Types (Bit Mask) 'corresponds to SERVICE_STATUS.dwServiceType Public Const SERVICE_KERNEL_DRIVER As Long = &H1 Public Const SERVICE_FILE_SYSTEM_DRIVER As Long = &H2 Public Const SERVICE_ADAPTER As Long = &H4 Public Const SERVICE_RECOGNIZER_DRIVER As Long = &H8 Public Const SERVICE_WIN32_OWN_PROCESS As Long = &H10 Public Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20 Public Const SERVICE_INTERACTIVE_PROCESS As Long = &H100 Public Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS Or _ SERVICE_WIN32_SHARE_PROCESS Public Const SERVICE_DRIVER As Long = SERVICE_KERNEL_DRIVER Or _ SERVICE_FILE_SYSTEM_DRIVER Or _ SERVICE_RECOGNIZER_DRIVER Public Const SERVICE_TYPE_ALL As Long = SERVICE_WIN32 Or _ SERVICE_ADAPTER Or _ SERVICE_DRIVER Or _ SERVICE_INTERACTIVE_PROCESS 'Service State 'corresponds to SERVICE_STATUS.dwCurrentState Public Const SERVICE_STOPPED As Long = &H1 Public Const SERVICE_START_PENDING As Long = &H2 Public Const SERVICE_STOP_PENDING As Long = &H3 Public Const SERVICE_RUNNING As Long = &H4 Public Const SERVICE_CONTINUE_PENDING As Long = &H5 Public Const SERVICE_PAUSE_PENDING As Long = &H6 Public Const SERVICE_PAUSED As Long = &H7 'Controls Accepted (Bit Mask) 'corresponds to SERVICE_STATUS.dwControlsAccepted Public Const SERVICE_ACCEPT_STOP As Long = &H1 Public Const SERVICE_ACCEPT_PAUSE_CONTINUE As Long = &H2 Public Const SERVICE_ACCEPT_SHUTDOWN As Long = &H4 'Windows type used to call the Net API Public Const MAX_PREFERRED_LENGTH As Long = -1 Public Const NERR_SUCCESS As Long = 0& Public Const SV_TYPE_WORKSTATION As Long = &H1 Public Const SV_TYPE_SERVER As Long = &H2 Public Const SV_TYPE_SQLSERVER As Long = &H4 Public Const SV_TYPE_DOMAIN_CTRL As Long = &H8 Public Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10 Public Const SV_TYPE_TIME_SOURCE As Long = &H20 Public Const SV_TYPE_AFP As Long = &H40 Public Const SV_TYPE_NOVELL As Long = &H80 Public Const SV_TYPE_DOMAIN_MEMBER As Long = &H100 Public Const SV_TYPE_PRINTQ_SERVER As Long = &H200 Public Const SV_TYPE_DIALIN_SERVER As Long = &H400 Public Const SV_TYPE_XENIX_SERVER As Long = &H800 Public Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER Public Const SV_TYPE_NT As Long = &H1000 Public Const SV_TYPE_WFW As Long = &H2000 Public Const SV_TYPE_SERVER_MFPN As Long = &H4000 Public Const SV_TYPE_SERVER_NT As Long = &H8000 Public Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000 Public Const SV_TYPE_BACKUP_BROWSER As Long = &H20000 Public Const SV_TYPE_MASTER_BROWSER As Long = &H40000 Public Const SV_TYPE_DOMAIN_MASTER As Long = &H80000 Public Const SV_TYPE_SERVER_OSF As Long = &H100000 Public Const SV_TYPE_SERVER_VMS As Long = &H200000 Public Const SV_TYPE_WINDOWS As Long = &H400000 'Win95 and above Public Const SV_TYPE_DFS As Long = &H800000 'Root of DFS tree Public Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT Cluster Public Const SV_TYPE_TERMINALSERVER As Long = &H2000000 'Terminal Server Public Const SV_TYPE_DCE As Long = &H10000000'IBM DSS Public Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000'rtn alternate transport Public Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000'rtn local only Public Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000 Public Const SV_TYPE_ALL As Long = &HFFFFFFFF Public Const SV_PLATFORM_ID_OS2 As Long = 400 Public Const SV_PLATFORM_ID_NT As Long = 500 'Mask applied to svX_version_major in 'order to obtain the major version number. Public Const MAJOR_VERSION_MASK As Long = &HF Public Type SERVER_INFO_100 sv100_platform_id As Long sv100_name As Long End Type Public Declare Function OpenSCManager Lib "advapi32" _ Alias "OpenSCManagerA" _ (ByVal lpMachineName As String, _ ByVal lpDatabaseName As String, _ ByVal dwDesiredAccess As Long) As Long Public Declare Function EnumServicesStatus Lib "advapi32" _ Alias "EnumServicesStatusA" _ (ByVal hSCManager As Long, _ ByVal dwServiceType As Long, _ ByVal dwServiceState As Long, _ lpServices As Any, _ ByVal cbBufSize As Long, _ pcbBytesNeeded As Long, _ lpServicesReturned As Long, _ lpResumeHandle As Long) As Long Public Declare Function CloseServiceHandle Lib "advapi32" _ (ByVal hSCObject As Long) As Long Public Declare Function NetServerEnum Lib "netapi32" _ (ByVal servername As Long, _ ByVal level As Long, _ buf As Any, _ ByVal prefmaxlen As Long, _ entriesread As Long, _ totalentries As Long, _ ByVal servertype As Long, _ ByVal domain As Long, _ resume_handle As Long) As Long Public Declare Function NetApiBufferFree Lib "netapi32" _ (ByVal Buffer As Long) As Long Public Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (pTo As Any, uFrom As Any, _ ByVal lSize As Long) Public Declare Function lstrlenW Lib "kernel32" _ (ByVal lpString As Long) As Long Public Declare Function lstrcpyA Lib "kernel32" _ (ByVal RetVal As String, ByVal Ptr As Long) As Long Public Declare Function lstrlenA Lib "kernel32" _ (ByVal Ptr As Any) As Long Public Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Public Function GetPointerToByteStringW(ByVal dwData As Long) As String Dim tmp() As Byte Dim tmplen As Long If dwData <> 0 Then tmplen = lstrlenW(dwData) * 2 If tmplen <> 0 Then ReDim tmp(0 To (tmplen - 1)) As Byte CopyMemory tmp(0), ByVal dwData, tmplen GetPointerToByteStringW = tmp End If End If End Function Public Function GetStrFromPtrA(ByVal lpszA As Long) As String GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0) Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA) End Function '--end block--'
Donc, si une machine fait tourner SQL server, logiquement il y a un service particulier (dont je connais pas le nom, va falloir faire un essai).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 Option Explicit Private lastIndex As Long Private Sub Combo1_Click() If Combo1.ListIndex > -1 Then Command1(lastIndex).Value = True End If End Sub Private Sub Command1_Click(Index As Integer) Dim sMachine As String If Combo1.ListIndex > -1 Then sMachine = Combo1.List(Combo1.ListIndex) Select Case Index Case 0: 'active services EnumSystemServices SERVICE_ACTIVE, sMachine, List1 Case 1: 'inactive services EnumSystemServices SERVICE_INACTIVE, sMachine, List1 Case 2: 'all services EnumSystemServices SERVICE_STATE_ALL, sMachine, List1 End Select 'save the current option to allow the list 'to refresh with the same type of data as 'different machines are selected. lastIndex = Index End If End Sub Private Sub Form_Load() ReDim TabArray(0 To 4) As Long TabArray(0) = 150 TabArray(1) = 220 TabArray(2) = 270 TabArray(3) = 342 TabArray(4) = 390 Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&) Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 5&, TabArray(0)) List1.Refresh Command1(0).Caption = "Active Services" Command1(1).Caption = "Inactive Services" Command1(2).Caption = "All Services" 'if you have a large list of machines, you may 'want to move this call to a command button to 'speed loading. Call GetServers(vbNullString) End Sub Public Function EnumSystemServices(SERVICE_TYPE As Long, _ sMachine As String, _ ctl As Control) As Long Dim hSCManager As Long Dim pntr() As ENUM_SERVICE_STATUS Dim cbBuffSize As Long Dim cbRequired As Long Dim dwReturned As Long Dim hEnumResume As Long Dim cbBuffer As Long Dim success As Long Dim i As Long 'these five just help keep the 'code lines from becoming too long 'for html display Dim sSvcName As String Dim sDispName As String Dim dwState As Long Dim dwType As Long Dim dwCtrls As Long 'update label to show what we're trying to view Label1.Caption = "Viewing " & Command1(SERVICE_TYPE - 1).Caption & " on " & sMachine 'establish a connection to the service control 'manager on the local computer and open 'the local service control manager database. hSCManager = OpenSCManager("\\" & sMachine, _ vbNullString, _ SC_MANAGER_ENUMERATE_SERVICE) If hSCManager <> 0 Then 'Get buffer size by calling EnumServicesStatus. 'To determine the required buffer size, call EnumServicesStatus 'with cbBuffer and hEnumResume set to zero. EnumServicesStatus 'fails (returns 0), and Err.LastDLLError returns ERROR_MORE_DATA, 'filling cbRequired with the size, in bytes, of the buffer 'required to hold the array of structures and their data. success = EnumServicesStatus(hSCManager, _ SERVICE_WIN32, _ SERVICE_TYPE, _ ByVal &H0, _ &H0, _ cbRequired, _ dwReturned, _ hEnumResume) 'If success is 0 and the LastDllError is 'ERROR_MORE_DATA, use returned info to create 'the required data buffer If success = 0 And Err.LastDllError = ERROR_MORE_DATA Then 'Calculate number of structures needed 'and redimension the array cbBuffer = (cbRequired \ SIZEOF_SERVICE_STATUS) + 1 ReDim pntr(0 To cbBuffer) 'Set cbBuffSize equal to the size of the buffer cbBuffSize = cbBuffer * SIZEOF_SERVICE_STATUS 'Enumerate the services. If the function succeeds, 'the return value is nonzero. If the function fails, 'the return value is zero. In addition, hEnumResume 'must be set to 0. hEnumResume = 0 If EnumServicesStatus(hSCManager, _ SERVICE_WIN32, _ SERVICE_TYPE, _ pntr(0), _ cbBuffSize, _ cbRequired, _ dwReturned, _ hEnumResume) Then 'pntr() array is now filled with service data, 'so it is a simple matter of extracting the 'required information. With ctl .Clear For i = 0 To dwReturned - 1 sDispName = GetStrFromPtrA(ByVal pntr(i).lpDisplayName) sSvcName = GetStrFromPtrA(ByVal pntr(i).lpServiceName) dwState = pntr(i).ServiceStatus.dwCurrentState dwType = pntr(i).ServiceStatus.dwServiceType dwCtrls = pntr(i).ServiceStatus.dwControlsAccepted .AddItem sDispName & vbTab & _ sSvcName & vbTab & _ GetServiceState(dwState) & vbTab & _ GetServiceType(dwType) & vbTab & _ GetServiceControl(dwCtrls) Next End With Else: MsgBox "EnumServicesStatus; error " & CStr(Err.LastDllError) End If 'If EnumServicesStatus Else: MsgBox "ERROR_MORE_DATA not returned; error " & CStr(Err.LastDllError) End If 'If success = 0 And Err.LastDllError Else: MsgBox "OpenSCManager failed; error = " & CStr(Err.LastDllError) End If 'If hSCManager <> 0 'Clean up Call CloseServiceHandle(hSCManager) 'return the number of services 'returned as a sign of success EnumSystemServices = dwReturned End Function Private Function GetServers(sDomain As String) As Long 'lists all servers of the specified type 'that are visible in a domain. Dim bufptr As Long Dim dwEntriesread As Long Dim dwTotalentries As Long Dim dwResumehandle As Long Dim se100 As SERVER_INFO_100 Dim success As Long Dim nStructSize As Long Dim cnt As Long nStructSize = LenB(se100) 'Call passing MAX_PREFERRED_LENGTH to have the 'API allocate required memory for the return values. ' 'The call is enumerating all machines on the 'network (SV_TYPE_ALL); however, by Or'ing 'specific bit masks for defined types you can 'customize the returned data. For example, a 'value of 0x00000003 combines the bit masks for 'SV_TYPE_WORKSTATION (0x00000001) and 'SV_TYPE_SERVER (0x00000002). ' 'dwServerName must be Null. The level parameter '(100 here) specifies the data structure being 'used (in this case a SERVER_INFO_100 structure). ' 'The domain member is passed as Null, indicating 'machines on the primary domain are to be retrieved. 'If you decide to use this member, pass 'StrPtr("domain name"), not the string itself. success = NetServerEnum(0&, _ 100, _ bufptr, _ MAX_PREFERRED_LENGTH, _ dwEntriesread, _ dwTotalentries, _ SV_TYPE_WORKSTATION Or SV_TYPE_SERVER, _ 0&, _ dwResumehandle) 'if all goes well If success = NERR_SUCCESS And _ success <> ERROR_MORE_DATA Then 'loop through the returned data, adding each 'machine to the list For cnt = 0 To dwEntriesread - 1 'get one chunk of data and cast 'into an LOCALGROUP_INFO_1 type 'in order to add the name to a list CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize Combo1.AddItem GetPointerToByteStringW(se100.sv100_name) Next End If 'clean up, regardless of success Call NetApiBufferFree(bufptr) End Function Public Function GetStrFromPtrA(ByVal lpszA As Long) As String GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0) Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA) End Function Private Function GetServiceControl(dwControl As Long) As String Dim tmp As String If dwControl Then If (dwControl And SERVICE_ACCEPT_STOP) Then tmp = tmp & "stop, " If (dwControl And SERVICE_ACCEPT_PAUSE_CONTINUE) Then tmp = tmp & "pause, " If (dwControl And SERVICE_ACCEPT_SHUTDOWN) Then tmp = tmp & "shutdown" 'Else: tmp = vbTab End If GetServiceControl = tmp End Function Private Function GetServiceType(dwType As Long) As String Dim sType As String If (dwType And SERVICE_WIN32_OWN_PROCESS) Then sType = sType & "own proc, " If (dwType And SERVICE_WIN32_SHARE_PROCESS) Then sType = sType & "share, " If (dwType And SERVICE_KERNEL_DRIVER) Then sType = sType & "kernel, " If (dwType And SERVICE_FILE_SYSTEM_DRIVER) Then sType = sType & "filesys, " If (dwType And SERVICE_INTERACTIVE_PROCESS) Then sType = sType & "interactive" GetServiceType = sType End Function Private Function GetServiceState(dwState As Long) As String Select Case dwState Case SERVICE_STOPPED: GetServiceState = "stopped" Case SERVICE_START_PENDING: GetServiceState = "startpend" Case SERVICE_STOP_PENDING: GetServiceState = "stoppend" Case SERVICE_RUNNING: GetServiceState = "running" Case SERVICE_CONTINUE_PENDING: GetServiceState = "contpend" Case SERVICE_PAUSE_PENDING: GetServiceState = "pausepend" Case SERVICE_PAUSED: GetServiceState = "paused" End Select End Function '--end block--'
A partir de là, l'idée va etre de modifier le code pour obtenir toutes les machine et service et ne garder que les machines qui disposent du service SQL server...
Bon, soyons honnetes: C'est pas evident, mais ca doit etre faisable...
Bon courage et bon developpement,
grafikm_fr
"L'éducation, c'est le début de la richesse, et la richesse n'est pas destinée à tout le monde" (Adolphe Thiers)
C'est parfais avec un Service ++.
Je pourais juste tester si le nom du sevice est MSSQLSERVEUR.
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager