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:
|
function GetSQLInstances(Instances: TStrings): Boolean;
function GetInstanceNameFromServiceName(const ServiceName: string): string;
const
SQLSERVER = 'MSSQLSERVER';
begin
if ServiceName = '' then
Result := ''
else
begin
if UpperCase(ServiceName) = SQLSERVER then
Result := ServiceName
else
Result := Copy(ServiceName, Pos('$', ServiceName)+1, Length(ServiceName)-Pos('$', ServiceName));
end;
end;
function GetWMIPropertyValueForEngineService(const PropertyName, ServiceName, WMINameSpace: string): string;
var
SWL: TSWbemLocator;
SWServ: ISWbemServices;
SWObjSet: ISWbemObjectSet;
Enum: IEnumVariant;
tempObj: OleVariant;
Value: Cardinal;
SWObj: ISWbemObject;
SWPropSet: ISWbemPropertySet;
sQry: string;
begin
SWL := TSWbemLocator.Create(nil);
try
try
SWServ := SWL.ConnectServer('', WMINameSpace, '', '', '', '', 0, nil);
sQry := Format('select * from SqlServiceAdvancedProperty where SQLServiceType = 1 and PropertyName = ''%s'' and ServiceName = ''%s''', [PropertyName, ServiceName]);
SWObjSet := SWServ.ExecQuery(sQry, 'WQL', wbemFlagReturnImmediately, nil);
if SWObjSet.Count = 0 then
begin
Result := '';
Exit;
end;
//
Enum := (SWObjSet._NewEnum) as IEnumVariant;
while (Enum.Next(1, tempObj, Value) = S_OK) do
begin
SWObj := IUnknown(tempObj) as SWBemObject;
SWPropSet := SWObj.Properties_;
Result := SWPropSet.Item('PropertyStrValue', 0).Get_Value;
end;
except
on E: Exception do
begin
//WriteLn('Error: ' + e.Message);
Result := '';
end;
end;
finally
SWL.Disconnect;
FreeAndNil(SWL);
end;
end;
const
WMI_NAMESPACES_TO_CHECK: array[0..1] of string = ('ComputerManagement', 'ComputerManagement10');
var
GetSqlExpress: TSWbemLocator;
Services: ISWbemServices;
ObjectSet: ISWbemObjectSet;
SObject: ISWbemObject;
propSet: ISWbemPropertySet;
Enum: IEnumVariant;
tempObj: OleVariant;
Value: Cardinal;
//
sQry: string;
serviceName: string;
WMIPath: string;
i: Integer;
begin
Result := False;
Instances.Clear;
for i := 0 to 1 do
begin
WMIPath := Format('root\Microsoft\SqlServer\%s', [WMI_NAMESPACES_TO_CHECK[i]]);
GetSqlExpress := TSWbemLocator.Create(nil);
try
try
Services := GetSqlExpress.ConnectServer('', WMIPath, '', '', '', '', 0, nil);
sQry := Format('SELECT * FROM SqlServiceAdvancedProperty WHERE SQLServiceType = 1 AND PropertyName = ''instanceID''', [{SQL_INSTANCE}]);
ObjectSet := Services.ExecQuery(sQry, 'WQL', wbemFlagReturnImmediately, nil);
if ObjectSet.Count = 0 then
begin
Continue;
end;
//
Enum := (ObjectSet._NewEnum) as IEnumVariant;
while (Enum.Next(1, tempObj, Value) = S_OK) do
begin
SObject := IUnknown(tempObj) as SWBemObject;
propSet := SObject.Properties_;
serviceName := propSet.Item('ServiceName', 0).Get_Value;
// Service, Instance, Version, Edition
Instances.Add(Format('%s, %s, %s, %s',
[serviceName,
GetInstanceNameFromServiceName(serviceName),
GetWMIPropertyValueForEngineService('Version', serviceName, WMIPath),
GetWMIPropertyValueForEngineService('SKUNAME', serviceName, WMIPath)]));
end;
Result := True;
except
on E: Exception do
begin
//WriteLn('Error: ' + e.Message);
Result := False;
end;
end;
finally
GetSqlExpress.Disconnect;
FreeAndNil(GetSqlExpress);
end;
end;
end;
|