如何查询远程机器是否包含某个文件夹(DELPHI)
2011-12-31 15:00
260 查看
最近接到用户的一个需求:查询给出计算机清单内这些计算机是否包含某个文件夹。当查询时,要对用户透明。说白了就是远程查询。想了想就用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.
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.
相关文章推荐
- Linux如何共享远程文件夹(日志服查询的实现方法)
- 如何知道远程的机器是否宕机
- WMIC 初体验--动态监听当前机器共享文件夹是否被远程访问
- 如何高效的查询数组中是否包含某个值
- 如何验证group by的查询SQL中是否包含重复数据
- 用模糊查询like语句时如果要查是否包含%字符串该如何写
- java删除整个文件夹或文件(无视是否包含子文件夹)
- 如何在shell脚本中判断文件或者文件夹是否存在?
- 用JAVA写查询一个字符串中是否包含另外一个字符串以及出现的次数
- adoquery查询结果如何赋给一个变量(delphi和c++ builder)
- 如何来判断你的Linux系统机器是否被黑?
- 如何判断字符串中是否包含汉字
- 转:如何高效地判断数组中是否包含某特定值
- 如何使用 syslog-ng 从远程 Linux 机器上收集日志 | Linux 中国
- PHP 远程图片:如何判断远程图片是否存在
- 如何测试远程服务器的某个端口是否打开?
- php如何判断文件是否存在,包括本地和远程文件
- solr入门之Solr函数查询初使用之查询字段是否包含指定内容
- 如何查看机器上是否有证书
- linux如何搜索一个文件夹下包含某个字段的文件