象運(yùn)行可執(zhí)行文件一樣,Powershell運(yùn)行文件和腳本,也必須使用絕對(duì)路徑或者相對(duì)路徑,或者要運(yùn)行的文件必須定義在可受信任的環(huán)境變量中。
關(guān)于腳本
腳本和批處理都屬于偽可執(zhí)行文件,它們只是包含了若干命令行解釋器能夠解釋和執(zhí)行的命令行代碼。
執(zhí)行批處理文件
批處理是擴(kuò)展名為”.bat”的文本文件,它可以包含任何cmd控制臺(tái)能夠處理的命令。當(dāng)批處理文件被打開(kāi),Cmd控制臺(tái)會(huì)逐行執(zhí)行每條命令。那Powershell能夠直接執(zhí)行批處理嗎?
將下列命令保存為ping.bat
@echo off
echo batch File Test
pause
Dir %windir%/system
然后執(zhí)行ping
屏幕會(huì)打印ping命令幫助,說(shuō)明調(diào)用的ping cmd 而不是ping.bat。
改為:
PS C:\PS> ./ping
batch File Test
Press any key to continue . . .
Volume in drive C has no label.
Volume Serial Number is 4E9B-D846
Directory of C:Windowssystem
2009/06/11 05:21 69,584 avicap.dll
2009/06/11 05:21 109,456 avifile.dll
2009/07/14 05:41 32,816 COMMDLG.DLL
2009/07/14 05:41 2,000 keyboard.drv
2009/06/11 05:42 9,936 lzexpand.dll
2009/06/11 05:21 73,376 mciavi.drv
2009/06/11 05:21 25,264 mciseq.drv
2009/06/11 05:21 28,160 mciwave.drv
2009/07/14 05:41 68,992 MMSYSTEM.DLL
2009/07/14 05:41 1,152 mmtask.tsk
2009/07/14 05:41 2,032 mouse.drv
2009/06/11 05:21 126,912 msvideo.dll
2009/06/11 05:42 82,944 olecli.dll
2009/07/14 05:41 24,064 OLESVR.DLL
2009/07/14 05:41 5,120 SHELL.DLL
2009/07/14 05:41 1,744 sound.drv
2009/06/11 05:25 5,532 stdole.tlb
2009/07/14 05:41 3,360 system.drv
2009/07/14 05:41 4,048 TIMER.DRV
2009/06/11 05:42 9,008 ver.dll
2009/07/14 05:41 2,176 vga.drv
2009/07/14 05:41 12,704 WFWNET.DRV
22 File(s) 700,380 bytes
2 Dir(s) 75,927,420,928 bytes free
這時(shí)運(yùn)行的是批處理。
通過(guò)cmd進(jìn)入cmd控制臺(tái)輸入ping發(fā)現(xiàn)執(zhí)行的不是ping命令,而是直接運(yùn)行ping.bat ,也就是說(shuō)可以通過(guò).bat 覆蓋cmd命令。這種機(jī)制很危險(xiǎn),如果有人侵入電腦,并將系統(tǒng)內(nèi)部命令篡改成自己批處理,那就太悲劇了。 這種命令與腳本的混淆不會(huì)發(fā)生在powershell中,因?yàn)閜owershell有更安全的機(jī)制。
執(zhí)行VB腳本文件
將下列命令保存為test.vbs
Set wmi = GetObject("winmgmts:")
Set collection = wmi.ExecQuery("select * from Win32_Process")
For Each process in collection
WScript.Echo process.getObjectText_
Next
執(zhí)行 .\test.vbs 會(huì)遍歷當(dāng)前Win32進(jìn)程,并把每個(gè)進(jìn)程的詳細(xì)信息通過(guò)窗口顯示出來(lái)。
怎樣讓VB腳本的通過(guò)控制臺(tái)輸出呢?
Wscript //H:CScript
怎樣還原VB腳本通過(guò)窗口輸出呢?
WScript //H:WScript
在powershell中執(zhí)行VB腳本
PS C:\PS> cscript.exe .test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (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";
};
執(zhí)行powershell腳本
Powershell擁有自己的腳本,擴(kuò)展名為“.ps1”
PS C:\PS> echo "dir;Get-PSProvider;help dir" >test.ps1
PS C:\PS> Get-Content ./test.ps1
dir;Get-PSProvider;help dir
PS C:\PS> ./test.ps1初次執(zhí)行腳本時(shí),可能會(huì)碰到一個(gè)異常:
File ” C:\PS\test.ps1″ cannot be loaded because the
execution of scripts is disabled on this system. Please see
“get-help about_signing” for more details.
At line:1 char:10
+ .test.ps1
這是powershell的默認(rèn)安全設(shè)置禁用了執(zhí)行腳本,要啟用這個(gè)功能需要擁有管理員的權(quán)限。
開(kāi)啟:set-executionpolicy remotesigned
關(guān)閉:Set-ExecutionPolicy Restricted
Powershell調(diào)用入口的優(yōu)先級(jí)
別名:控制臺(tái)首先會(huì)尋找輸入是否為一個(gè)別名,如果是,執(zhí)行別名所指的命令。因此我們可以通過(guò)別名覆蓋任意powershell命令,因?yàn)閯e名的優(yōu)先級(jí)最高。
函數(shù):如果沒(méi)有找到別名,會(huì)繼續(xù)尋找函數(shù),函數(shù)類似別名,只不過(guò)它包含了更多的powershell命令。因此可以自定義函數(shù)擴(kuò)充cmdlet 把常用的參數(shù)給固化進(jìn)去。
命令:如果沒(méi)有找到函數(shù),控制臺(tái)會(huì)繼續(xù)尋找命令,即cmdlet,powershell的內(nèi)部命令。
腳本:沒(méi)有找到命令,繼續(xù)尋找擴(kuò)展名為“.ps1”的Powershell腳本。
文件:沒(méi)有找到腳本,會(huì)繼續(xù)尋找文件,如果沒(méi)有可用的文件,控制臺(tái)會(huì)拋出異常。
The term ‘now' is not recognized as the name of a cmdlet, function, script file, or operable program. Chec
g 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
您可能感興趣的文章:- PowerShell中獲取Windows系統(tǒng)序列號(hào)的腳本分享
- PowerShell腳本中控制Windows DNS服務(wù)的方法
- windows下寫(xiě)的shell腳本在linux執(zhí)行出錯(cuò)的解決辦法
- linux和windows下的自動(dòng)ftp腳本(shell bat)
- 下一代Shell腳本語(yǔ)言,Windows Power Shell!
- windows下上傳shell腳本不能運(yùn)行的解決方法