Windows Powershell 执行文件和脚本
象运行可执行文件一样,Powershell运行文件和脚本,也必须使用绝对路径或者相对路径,或者要运行的文件必须定义在可受信任的环境变量中。
关于脚本
脚本和批处理都属于伪可执行文件,它们只是包含了若干命令行解释器能够解释和执行的命令行代码。
执行批处理文件
批处理是扩展名为”.bat”的文本文件,它可以包含任何cmd控制台能够处理的命令。当批处理文件被打开,Cmd控制台会逐行执行每条命令。那Powershell能够直接执行批处理吗?
将下列命令保存为ping.bat
@echooff echobatchFileTest pause Dir%windir%/system
然后执行ping
屏幕会打印ping命令帮助,说明调用的pingcmd而不是ping.bat。
改为:
PSC:\PS>./ping batchFileTest Pressanykeytocontinue... VolumeindriveChasnolabel. VolumeSerialNumberis4E9B-D846 DirectoryofC:Windowssystem 2009/06/1105:2169,584avicap.dll 2009/06/1105:21109,456avifile.dll 2009/07/1405:4132,816COMMDLG.DLL 2009/07/1405:412,000keyboard.drv 2009/06/1105:429,936lzexpand.dll 2009/06/1105:2173,376mciavi.drv 2009/06/1105:2125,264mciseq.drv 2009/06/1105:2128,160mciwave.drv 2009/07/1405:4168,992MMSYSTEM.DLL 2009/07/1405:411,152mmtask.tsk 2009/07/1405:412,032mouse.drv 2009/06/1105:21126,912msvideo.dll 2009/06/1105:4282,944olecli.dll 2009/07/1405:4124,064OLESVR.DLL 2009/07/1405:415,120SHELL.DLL 2009/07/1405:411,744sound.drv 2009/06/1105:255,532stdole.tlb 2009/07/1405:413,360system.drv 2009/07/1405:414,048TIMER.DRV 2009/06/1105:429,008ver.dll 2009/07/1405:412,176vga.drv 2009/07/1405:4112,704WFWNET.DRV 22File(s)700,380bytes 2Dir(s)75,927,420,928bytesfree
这时运行的是批处理。
通过cmd进入cmd控制台输入ping发现执行的不是ping命令,而是直接运行ping.bat,也就是说可以通过.bat覆盖cmd命令。这种机制很危险,如果有人侵入电脑,并将系统内部命令篡改成自己批处理,那就太悲剧了。这种命令与脚本的混淆不会发生在powershell中,因为powershell有更安全的机制。
执行VB脚本文件
将下列命令保存为test.vbs
Setwmi=GetObject("winmgmts:")
Setcollection=wmi.ExecQuery("select*fromWin32_Process")
ForEachprocessincollection
WScript.Echoprocess.getObjectText_
Next
执行.\test.vbs会遍历当前Win32进程,并把每个进程的详细信息通过窗口显示出来。
怎样让VB脚本的通过控制台输出呢?
Wscript//H:CScript
怎样还原VB脚本通过窗口输出呢?
WScript//H:WScript
在powershell中执行VB脚本
PSC:\PS>cscript.exe.test.vbs
Microsoft(R)WindowsScriptHostVersion5.8
Copyright(C)MicrosoftCorporation.Allrightsreserved.
instanceofWin32_Process
{
Caption="SystemIdleProcess";
CreationClassName="Win32_Process";
CSCreationClassName="Win32_ComputerSystem";
CSName="test-me-01";
Description="SystemIdleProcess";
Handle="0";
HandleCount=0;
KernelModeTime="484113379271";
Name="SystemIdleProcess";
OSCreationClassName="Win32_OperatingSystem";
OSName="MicrosoftWindows7Enterprise|C:Windows|DeviceHarddisk0Partition2";
OtherOperationCount="0";
OtherTransferCount="0";
PageFaults=0;
PageFileUsage=0;
ParentProcessId=0;
PeakPageFileUsage=0;
PeakVirtualSize="0";
PeakWorkingSetSize=0;
Priority=0;
PrivatePageCount="0";
ProcessId=0;
QuotaNonPagedPoolUsage=0;
QuotaPagedPoolUsage=0;
QuotaPeakNonPagedPoolUsage=0;
QuotaPeakPagedPoolUsage=0;
ReadOperationCount="0";
ReadTransferCount="0";
SessionId=0;
ThreadCount=2;
UserModeTime="0";
VirtualSize="0";
WindowsVersion="6.1.7601";
WorkingSetSize="24576";
WriteOperationCount="0";
WriteTransferCount="0";
};
执行powershell脚本
Powershell拥有自己的脚本,扩展名为“.ps1”
PSC:\PS>echo"dir;Get-PSProvider;helpdir">test.ps1 PSC:\PS>Get-Content./test.ps1 dir;Get-PSProvider;helpdir PSC:\PS>./test.ps1初次执行脚本时,可能会碰到一个异常: File”C:\PS\test.ps1″cannotbeloadedbecausethe executionofscriptsisdisabledonthissystem.Pleasesee “get-helpabout_signing”formoredetails. Atline:1char:10 +.test.ps1<<<<
这是powershell的默认安全设置禁用了执行脚本,要启用这个功能需要拥有管理员的权限。
开启:set-executionpolicyremotesigned
关闭:Set-ExecutionPolicyRestricted
Powershell调用入口的优先级
别名:控制台首先会寻找输入是否为一个别名,如果是,执行别名所指的命令。因此我们可以通过别名覆盖任意powershell命令,因为别名的优先级最高。
函数:如果没有找到别名,会继续寻找函数,函数类似别名,只不过它包含了更多的powershell命令。因此可以自定义函数扩充cmdlet把常用的参数给固化进去。
命令:如果没有找到函数,控制台会继续寻找命令,即cmdlet,powershell的内部命令。
脚本:没有找到命令,继续寻找扩展名为“.ps1”的Powershell脚本。
文件:没有找到脚本,会继续寻找文件,如果没有可用的文件,控制台会抛出异常。
Theterm‘now'isnotrecognizedasthenameofacmdlet,function,scriptfile,oroperableprogram.Chec gofthename,orifapathwasincluded,verifythatthepathiscorrectandtryagain. Atline:1char:4 +now<<<< +CategoryInfo:ObjectNotFound:(now:String)[],CommandNotFoundException +FullyQualifiedErrorId:CommandNotFoundException