像运行可执行文件一样,Powershell运行文件和脚本,也必须使用绝对路径或者相对路径,或者要运行的文件必须定义在可受信任的环境变量中。
关于脚本
脚本和批处理都属于伪可执行文件,它们只是包含了若干命令行解释器能够解释和执行的命令行代码。
执行批处理文件
批处理是扩展名为”.bat”的文本文件,它可以包含任何cmd控制台能够处理的命令。当批处理文件被打开,Cmd控制台会逐行执行每条命令。那Powershell能够直接执行批处理吗?
将下列命令保存为ping.bat
@echo offecho batch File Testpause
然后执行ping
屏幕会打印ping命令帮助,说明调用的ping cmd 而不是ping.bat。
改为:
PS C:\PS> ./pingbatch File TestPress any key to continue . . .
这时运行的是批处理。
通过cmd进入cmd控制台输入ping发现执行的不是ping命令,而是直接运行ping.bat ,也就是说可以通过.bat 覆盖cmd命令。这种机制很危险,如果有人侵入电脑,并将系统内部命令篡改成自己批处理,那就太悲剧了。 这种命令与脚本的混淆不会发生在powershell中,因为powershell有更安全的机制。
执行VB脚本文件
将下列命令保存为 test.vbs
Set wmi = GetObject("winmgmts:")Set collection = wmi.ExecQuery("select * from Win32_Process")For Each process in collectionWScript.Echo process.getObjectText_Next
执行 .\test.vbs 会遍历当前Win32进程,并把每个进程的详细信息通过窗口显示出来。
通过窗口输出VB脚本
wscript test.vbs
在powershell中执行VB脚本
PS C:\PS> cscript test.vbsMicrosoft (R) Windows Script Host Version 5.8Copyright (C) Microsoft Corporation. All rights reserved.instance of Win32_Process{Caption = "System Idle Process";CreationClassName = "Win32_Process";CSCreationClassName = "Win32_ComputerSystem";CSName = "test-me-01";Description = "System Idle Process";Handle = "0";HandleCount = 0;KernelModeTime = "484113379271";Name = "System Idle Process";OSCreationClassName = "Win32_OperatingSystem";OSName = "Microsoft Windows 7 Enterprise |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”
PS C:\PS> echo "dir;Get-PSProvider;help dir" >test.ps1PS C:\PS> Get-Content ./test.ps1dir;Get-PSProvider;help dirPS C:\PS> ./test.ps1
初次执行脚本时,可能会碰到一个异常:
File ” C:\PS\test.ps1″ cannot be loaded because theexecution of scripts is disabled on this system. Please see“get-help about_signing” for more details.At line:1 char:10\+ .test.ps1 <<<<
这是powershell的默认安全设置禁用了执行脚本,要启用这个功能需要拥有管理员的权限。
Powershell调用入口的优先级
别名:控制台首先会寻找输入是否为一个别名,如果是,执行别名所指的命令。因此我们可以通过别名覆盖任意powershell命令,因为别名的优先级最高。
函数:如果没有找到别名,会继续寻找函数,函数类似别名,只不过它包含了更多的powershell命令。因此可以自定义函数扩充cmdlet 把常用的参数给固化进去。
命令:如果没有找到函数,控制台会继续寻找命令,即cmdlet,powershell的内部命令。
脚本:没有找到命令,继续寻找扩展名为“.ps1”的Powershell脚本。
文件:没有找到脚本,会继续寻找文件,如果没有可用的文件,控制台会抛出异常。
The term ‘now’ is not recognized as the name of a cmdlet, function, script file, or operable program. Checg of the name, or if a path was included, verify that the path is correct and try again.At line:1 char:4\+ now <<<<\+ CategoryInfo : ObjectNotFound: (now:String) [], CommandNotFoundException\+ FullyQualifiedErrorId : CommandNotFoundException
