关于PowerShell我不想在这里多说什么,这个已经是去年出现的东东了。它目前已经发展到了PowerShell 2 CTP2阶段了,有兴趣的朋友可以通过一个Jeffrey Snover(PowerShell的总架构师)的采访来了解一下。
最近有点点无聊,就拿它出来研究一下,补一补Windows方面日渐匮乏的知识。于是就下了一本PowerShell的官方宝典PowerShell Help打算好好专研一下,谁知道翻开宝典的第一页就碰壁了,让我颇为郁闷。你还别说这个宝典还真不错,开篇第一页就先教你一个小技巧,而不像其它那些一开篇总是一堆无用的说明。这个小技巧嘛就是教你在PowerShell中添加一个可以让你随时在程序中调出这个宝典的Get-Guihelp函数。好,废话不多说了,各位看官请接着往下看。
问题
当你按照PowerShell Help的指示添加"Windows.PowerShell_Profile.ps1"到"C:\Documents and Settings\XXX\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"中后,退出PowerShell然后再重新启动,按道理我们应该就可以很惬意的使用刚刚我们添加上的函数调出PowerShell Help查找我们需要的东西了。可是事实并不是这样,当你重新打开PowerShell将会获得以下的提示:
File C:\Documents and Settings\XXX\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.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:23
+ C:\Documents and Settings\XXX\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 <<<<
原因
没办法,官方这都没有指示,最快捷的办法就是用Google了。原来PowerShell默认的执行权限是受限的,所以我们的函数在启动的时候没有被执行。在PowerShell中有4中执行权限:
- Restricted——默认的设置, 不允许任何script运行
- AllSigned——只能运行经过数字证书签名的script
- RemoteSigned——运行本地的script不需要数字签名,但是运行从网络上下载的script就必须要有数字签名
- Unrestricted——允许所有的script运行
解决办法
你可以使用"Set-ExecutionPolicy"cmdlet来改变的你PowerShell环境。例如,你可以使用如下命令让PowerShell运行在无限制的环境之下:
Set-ExecutionPolicy Unrestricted
不过有一点非常重要的需要提醒大家,无限制的运行环境千万不要使用在生产环境之下,因为它将可能允许scripts从网络上下载或者植入木马病毒执行!


Nobody has left a comment!