【发布时间】:2016-03-22 22:15:19
【问题描述】:
我只是在询问社区是否有办法让脚本在执行脚本之前检查正在运行的 POSH 版本。目前,我的解决方法是以下代码:
#region Final Checks
#//Check to make sure that version of PowerShell is at least 3.0 before preceding.
If($PSVersionTable.PSVersion.Major -le 2) {
Throw "This script has not been tested with version 2.0 or older of PowerShell. Please execute this script from a system that has PowerShell 3.0 or newer installed. Windows 8/Server 2012 and newer has it installed by default. Windows 7/Server 2008 R2 can be patched to have 3.0 installed."
}
#endregion Final Checks
我在定义我的参数后就拥有了这个权利。但是,为了我自己的疯狂,我希望脚本在进入脚本的肉和土豆之前自动执行此检查。一个很好的比较是使用 Validate[X] 作为参数。如果操作员尝试提供不适合我的用户的数据,则会在执行脚本之前引发错误。有任何想法吗?我知道 [CmdletBinding()] 中没有任何东西可以做到这一点。谢谢!
【问题讨论】:
标签: powershell powershell-3.0 powershell-ise