象運行可執行文件一樣,Powershell運行文件和腳本,也必須使用絕對路徑或者相對路徑,或者要運行的文件必須定義在可受信任的環境變量中。
關于腳本
腳本和批處理都屬于偽可執行文件,它們只是包含了若干命令行解釋器能夠解釋和執行的命令行代碼。
執行批處理文件
批處理是擴展名為”.bat”的文本文件,它可以包含任何cmd控制臺能夠處理的命令。當批處理文件被打開,Cmd控制臺會逐行執行每條命令。那Powershell能夠直接執行批處理嗎?
將下列命令保存為ping.bat
@echo offecho batch File TestpauseDir %windir%/system
然后執行ping
屏幕會打印ping命令幫助,說明調用的ping cmd 而不是ping.bat。
改為:
PS C:/PS> ./pingbatch File TestPress any key to continue . . . Volume in drive C has no label. Volume Serial Number is 4E9B-D846 Directory of C:Windowssystem2009/06/11 05:21 69,584 avicap.dll2009/06/11 05:21 109,456 avifile.dll2009/07/14 05:41 32,816 COMMDLG.DLL2009/07/14 05:41 2,000 keyboard.drv2009/06/11 05:42 9,936 lzexpand.dll2009/06/11 05:21 73,376 mciavi.drv2009/06/11 05:21 25,264 mciseq.drv2009/06/11 05:21 28,160 mciwave.drv2009/07/14 05:41 68,992 MMSYSTEM.DLL2009/07/14 05:41 1,152 mmtask.tsk2009/07/14 05:41 2,032 mouse.drv2009/06/11 05:21 126,912 msvideo.dll2009/06/11 05:42 82,944 olecli.dll2009/07/14 05:41 24,064 OLESVR.DLL2009/07/14 05:41 5,120 SHELL.DLL2009/07/14 05:41 1,744 sound.drv2009/06/11 05:25 5,532 stdole.tlb2009/07/14 05:41 3,360 system.drv2009/07/14 05:41 4,048 TIMER.DRV2009/06/11 05:42 9,008 ver.dll2009/07/14 05:41 2,176 vga.drv2009/07/14 05:41 12,704 WFWNET.DRV 22 File(s) 700,380 bytes 2 Dir(s) 75,927,420,928 bytes free
這時運行的是批處理。
通過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 //H:CScript
怎樣還原VB腳本通過窗口輸出呢?
WScript //H:WScript
在powershell中執行VB腳本
PS C:/PS> cscript.exe .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的默認安全設置禁用了執行腳本,要啟用這個功能需要擁有管理員的權限。
開啟:set-executionpolicy remotesigned
關閉:Set-ExecutionPolicy Restricted
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
新聞熱點
疑難解答
圖片精選