Convert value to corresponding text

I am trying to get my script complete for checking powershell versions on remote machines and now I got to the last part, I am getting the file version number back from powershell, but I am looking for a way to transition 6.2.1200.XXX to version 3 my script so far

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


source to share


2 answers


The file versions that include the revision may change as updates are installed, but the first 3 numbers should be helpful. You can use like [version]

or just use a simple partition or replacement to get rid of the build.

Then you can make a hash table with version numbers as keys and PS versions as values.

$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]

      

Otherwise, if you've determined that PowerShell remote control is actually enabled, another way is to simply ask it:

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

      

Installation alternatives $myVer

:

String substitution:



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

      

Replace (regex):

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

      

Split with Range:

$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

      

Usage switch -wildcard

(credit to Ansgar Wiechers):

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

      

+4


source


The code that ended is



[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 { }
}

      

0


source







All Articles