支持所有PS版本
Powershell處理可執(zhí)行程序(如EXE)類似其它語言。然而你也可以讓PS阻止執(zhí)行任何程序或僅允許執(zhí)行授權(quán)文件。
默認(rèn)是允許執(zhí)行任何程序:
復(fù)制代碼 代碼如下:
PS> $ExecutionContext.SessionState.Applications
*
下面將授權(quán)PS只允許執(zhí)行ping.exe和regedit.exe命令。
復(fù)制代碼 代碼如下:
$ExecutionContext.SessionState.Applications.Clear()
$ExecutionContext.SessionState.Applications.Add('ping.exe')
$ExecutionContext.SessionState.Applications.Add('regedit.exe')
請(qǐng)看結(jié)果:
復(fù)制代碼 代碼如下:
$ExecutionContext.SessionState.Applications
ping.exe
regedit.exe
比如,我此時(shí)執(zhí)行ipconfig時(shí)就應(yīng)當(dāng)報(bào)錯(cuò):
復(fù)制代碼 代碼如下:
PS> ipconfig
ipconfig : 無法將“ipconfig.exe”項(xiàng)識(shí)別為 cmdlet、函數(shù)、腳本文件或可運(yùn)行程序的名稱。請(qǐng)檢查名稱的拼寫,如果包括路徑,請(qǐng)
確保路徑正確,然后再試一次。
所在位置 行:1 字符: 1
+ ipconfig
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ipconfig.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
當(dāng)然,你也可以還原到初始設(shè)置:
復(fù)制代碼 代碼如下:
$ExecutionContext.SessionState.Applications.Add('*')
PS> explorer
PS>
所以,它能較好的防止EXE的執(zhí)行(或意外執(zhí)行非法EXE),使用它作為一種安全模式,你就可以關(guān)閉。當(dāng)關(guān)閉了它,你將不能執(zhí)行訪問.net對(duì)象,于是在當(dāng)前會(huì)話你將不能在恢復(fù)這些設(shè)置。