现在PowerShell已经更新到了5.0版本了,因为不同的版本都会有不同的新增功能和命令,所以当我们所写的脚本是需要我们在具备特定的环境中运行的,而你的脚本又可能被别人拿去使用,为了保证在特定的环境中使用,我们必须做一些前提条件,以此防止使用你脚本的人遇到各类并非脚本本身功能缺陷的问题。
在PowerShell中其实可以用#Requires方式去特定某些运行条件,大致语法如下:
#Requires -Version <N>[.<n>]
#Requires –PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
#Requires -Modules { <Module-Name> | <Hashtable> }
#Requires –ShellId <ShellId>
#Requires -RunAsAdministrator
这里要注意的是,Requires的语句必须放在脚本的第一行,像下面的例子.
#Requires -runasadministrator
#Requires –Version 3
Param
(
[parameter(Mandatory=$true)]
[String[]]
$Path
)
...
这个例子就说明了,要求PowerShell脚本必须以管理员权限运行,并且版本必须不能低于PowerShell 3.0