最近接到用户的一个需求:查询给出计算机清单内这些计算机是否包含某个文件夹。当查询时,要对用户透明。说白了就是远程查询。想了想就用WMI来做,下面是相应的代码,为了简单,用了第三方控件
unit DirectoryCheck;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSkinStore, WinSkinData, ComCtrls, StdCtrls, cxControls,
cxContainer, cxEdit, cxTextEdit, cxMaskEdit, Mask, RzEdit, DB,
WmiConnection, WmiDataSet, IdBaseComponent, IdComponent, IdRawBase,
IdRawClient, IdIcmpClient,ComObj;
type
TForm1 = class(TForm)
SkinData1: TSkinData;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
RzMaskEdit1: TRzMaskEdit;
Button1: TButton;
WmiQuery1: TWmiQuery;
WmiConnection1: TWmiConnection;
IdIcmpClient1: TIdIcmpClient;
Label4: TLabel;
Edit3: TEdit;
Function PingResult(HostName:String):String;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function Tform1.PingResult(HostName:String):String;
Begin
IdIcmpClient1.Host:=HostName;
Try
IdIcmpClient1.Ping;
if (IdIcmpClient1.replyStatus.BytesReceived=0) or (IdIcmpClient1.ReplyStatus.TimeToLive=0) then
begin
Result:='该机器不存在或不在线或防火墙开着';
end else
begin
Result:='该机器存在';
end
Except
Result:='该机器不存在';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var account,pwd,path,Hostname,directoryname,SQLstring,addvalue:string;
introw,i,k:Integer;
ExcelApp:Variant;
begin
//directoryname:='%'+StringReplace(Trim(EDIT2.Text),'\','\\',[rfReplaceAll]);
//directoryname:='%'+Trim(Edit2.Text);
directoryname:='%'+'Crack';
path:=Trim(edit3.Text);
account:=Trim(edit1.Text);
pwd:=Trim(RzMaskEdit1.Text);
Introw:=2;
ExcelApp:=CreateoleObject('Excel.Application');
ExcelApp.Visible:=True;
ExcelApp.Workbooks.Open(Path);
ExcelApp.Worksheets[1].Activate;
ExcelApp.ActiveSheet.Rows[1].Font.Bold := True;
Excelapp.activeSheet.Columns[2].ColumnWidth:=34;
Excelapp.activeSheet.Columns[3].ColumnWidth:=70;
ExcelApp.cells[1,1].value:='Host Name';
ExcelApp.cells[1,2].value:='Ping 的状态';
ExcelApp.cells[1,3].value:='检测结果';
ExcelApp.cells[1,4].value:='Comments';
//ExcelApp.cells[1,3].value:=null;
While not (ExcelApp.Cells[introw,1].value ='') do
begin
ExcelApp.cells[introw,2].value:=form1.PingResult(ExcelApp.cells[introw,1].value);
if ExcelApp.cells[introw,2].value ='该机器存在' then
begin
Hostname:=Excelapp.cells[introw,1].value;
SQLstring:='select * from Win32_Directory where name like '+''''+directoryname+'''';
Wmiconnection1.Credentials.UserName:=account;
Wmiconnection1.Credentials.Password:=pwd;
wmiconnection1.MachineName:=Hostname;
try
wmiconnection1.connected:=true;
wmiquery1.WQL.Clear;
wmiquery1.WQL.Add(SQLstring);
wmiquery1.Active:=true;
Wmiquery1.Open;
k:=0;
if wmiquery1.RecordCount>0 then
begin
wmiquery1.First;
// k:=0;
for i:=k to wmiquery1.RecordCount-1 do
begin
addvalue:=wmiquery1.FieldByName('Name').Value;
if k<1 then
begin
Excelapp.cells[introw,3].value:=addvalue+Chr(10);
wmiquery1.Next;
end else
begin
Excelapp.cells[introw,3].value:=Excelapp.cells[introw,3].value+addvalue+Chr(10);
wmiquery1.Next;
end;
k:=k+1;
end;
introw:=introw+1;
end else
begin
Excelapp.cells[introw,3].value:='没有这个目录';
introw:=introw+1;
end;
wmiquery1.Close;
except
ExcelApp.cells[introw,4].value:='相应端口无法打开';
introw:=introw+1;
end;
end else
introw:=introw+1;
end;
ExcelApp.ActiveWorkbook.Save;
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
Excelapp:=unassigned;
Showmessage('完成检查');
end;
end.