象運行可執行檔一樣,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