我们正在为客户端提供脚本,这些脚本只能在Powershell 2.0中运行。
通过命令,我们可以确保Powershell2.0安装如下
$version = Get-host | Select-object Version但是,如果我们提供脚本,如何确保他们从Powershell 2.0执行该脚本?
执行脚本时,Powershell 2.0特性可能会在启动脚本本身时给出脚本错误。是不是?
发布于 2012-08-02 14:03:21
依赖主机版本并不是最安全的做法,因为脚本可以在与PowerShell版本不同的主机上运行。
在v2中添加了$PSVersionTable变量。您可以检查变量是否存在,如果存在,您正在运行v2或更高版本,您还可以检查PSVersion.Major属性:
if($PSVersionTable)
{
'PowerShell {0}' -f $PSVersionTable.PSVersion.Major
}
else
{
'PowerShell 1'
}发布于 2012-08-02 20:23:12
我会这样做:
# get version
$ver = $PsVersionTable.psversion.major
# Check to see if it's V2
If ($ver -NE 2)
{"incorrect version - exiting; return}
# Everything that follows is V2希望这能有所帮助!
发布于 2012-08-02 13:49:16
尝试使用$PSVersionTable
您将得到类似如下的内容:
Name Value
---- -----
CLRVersion 2.0.50727.5456
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1https://stackoverflow.com/questions/11771605
复制相似问题