将值转换为相应的文本

Converting a value to corresponding text

我正在尝试完成我的脚本以检查远程机器上的 powershell 版本,我现在进入最后一部分,我正在从 powershell 获取文件的版本号,但我正在寻找一种方法将 6.2.1200.XXX 转换为版本 3,到目前为止我的脚本是

Foreach ($Computer in $Computers)
{
    Try
    {
        Write-Host "Checking Computer $Computer"
        $path = "\$Computer\C$\windows\System32\WindowsPowerShell\v1.0\powershell.exe"
        if (test-path $path)
        {
            Write-host "Powershell is installed::"
            [bool](ls $path).VersionInfo
            Write-host " "
            Write-host "Powershell Remoting Enabled::"
            [bool](Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue)
        }
        else
        {
            Write-Host "Powershell isn't installed" -ForegroundColor 'Red'
        }
        Write-Host "Finished Checking Computer $Computer"
    }

包含修订的文件版本可能会随着更新的安装而改变,但前 3 个数字应该有用。您可以转换为 [version] 或者您可以只使用简单的拆分或替换来摆脱构建。

然后您可以创建一个哈希表,其中版本号作为键,PS 版本作为值。

$fileVer = [version](Get-Item $path).VersionInfo
$myVer = "{0}.{1}.{2}" -f $fileVer.Major,$fileVer.Minor,$fileVer.Build

$verTable = @{
    '6.3.1200' = 3
    '6.3.9600' = 4
}

$psVer = $verTable[$myVer]

否则,如果您确定 PowerShell 远程处理实际上已启用,另一种方法是直接询问它:

$prEnabled = [bool](Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue)
if ($prEnabled) {
    $psVer = Invoke-Command -ComputerName $computer -ScriptBlock { $PSVersionTable.PSVersion.Major }
}

备选设置 $myVer

字符串替换:

$fileVer = [version](Get-Item $path).VersionInfo
$myVer = "$($fileVer.Major).$($fileVer.Minor).$($fileVer.Build)"

替换(正则表达式):

$fileVer = (Get-Item $path).VersionInfo
$myVer = $fileVer -replace '\.\d+$',''
# replaces the last dot and any digits with nothing

拆分范围:

$fileVer = (Get-Item $path).VersionInfo
$myVer = ($fileVer -split '\.')[0..2]
# splits on a literal dot, then uses a range to get the first 3 elements of the array

使用 switch -wildcard(归功于 Ansgar Wiechers):

$fileVer = (Get-Item $path).VersionInfo.ProductVersion
$myVer = switch -Wildcard ($fileVer) {
    '6.3.1200.*' { 3 }
    '6.3.9600.*' { 4 }
}

我结束的代码是

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    $ComputerName
)
if (Test-Path $ComputerName)
{
    $Computers = Get-Content $ComputerName
}
Else
{
    $Computers = $ComputerName
}

Foreach ($Computer in $Computers)
{
    Try
    {
        Write-Host "Checking Computer $Computer"
        $path = "\$Computer\C$\windows\System32\WindowsPowerShell\v1.0\powershell.exe"
        $fileVer = (Get-Item $path).VersionInfo.ProductVersion
        $myVer = switch -Wildcard ($fileVer)
        {
            '6.0.6002.*' { 1 }
            '6.1.7600.*' { 2 }
            '6.2.9200.*' { 3 }
            '6.3.9600.*' { 4 }
        }

        if (test-path $path)
        {
            Write-host "Powershell is installed::"
            [bool](ls $path).VersionInfo
            Write-host " "
            Write-host "Powershell Version installed::"
            Write-host " $myVer "
            Write-host " "
            Write-host "Powershell Remoting Enabled::"
            [bool](Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue)
        }
        else
        {
            Write-Host "Powershell isn't installed" -ForegroundColor 'Red'
        }
        Write-Host "Finished Checking Computer $Computer"
    }

    catch { }
}