当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 版本配置文件

作者:精品下载站 日期:2024-12-14 07:38:52 浏览:14 分类:玩电脑

PowerShell 版本配置文件


[玩转系统] PowerShell 版本配置文件

基本上,我的配置文件脚本在执行任何操作之前首先检查版本。您可以使用 $psversiontable 变量。


PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      3.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.18033
BuildVersion                   6.2.9200.16434
PSCompatibleVersions           {1.0, 2.0, 3.0}
PSRemotingProtocolVersion      2.2

PSVersion 属性就是我正在寻找的。有了这些信息,我就可以将 PowerShell 配置文件脚本封装在简单的 IF 语句中。

#requires -version 2.0

<#
Use code like this in your PowerShell profile if it will
be shared between PowerShell 2 and PowerShell 3 sessions
on the same computer
#>

if ($psversiontable.psversion -eq '3.0') {
 Write-Host "You are running PowerShell 3.0" -ForegroundColor Green
 #insert 3.0 specific commands
 $PSDefaultParameterValues.Add("Format-Table:Autosize",$True)
}
else {
  Write-Host "You are running PowerShell 2.0" -ForegroundColor Yellow
  #insert 2.0 specific commands
}

#insert commands that apply to both versions

我认为代码示例中的注释非常清楚,实际上没有什么可添加的。写入主机线仅用于测试。你并不真的需要它们。

现在,无论版本如何,我都可以正确配置 PowerShell 会话,并且不会出现错误。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯