Partager via


Windows - what version of PS is on the machines?

function GET-PSVERSIONINFO ([string[]] $TargetComputers) {

  $HKLM = [Microsoft.Win32.RegistryHive]::LocalMachine
  $PowerShellRegistry="SOFTWARE\Microsoft\PowerShell"
  $KeyItem = "\PowerShellEngine"
  $Version = "PowerShellVersion"
 
  $OldErrorActionPreference = $ErrorActionPreference
  $ErrorActionPreference = “SilentlyContinue”
#  if I cannot connect, don't show the error, just continue onto the next one.

  foreach ($computer in $TargetComputers)  {
    $available = Test-Connection -ComputerName $computer -Quiet -Count 1
    if ($available)  {
     $psVersions=$null
     $Registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($HKLM,$computer)
     if ($Registry -ne $null)  {
      $PSRoot = $Registry.OpenSubKey($PowerShellRegistry)
      if ($PSRoot.SubKeyCount -gt 0) {
       foreach ($SubKey in $PSRoot.GetSubKeyNames()) {
         $PowerShellEngineKey = "$PowerShellRegistry\$SubKey$KeyItem"
         $EngineKey = $Registry.OpenSubKey($PowerShellEngineKey)
         if ($EngineKey -ne $null)  {
          [string]$vData = $EngineKey.GetValue($Version)
          if ($psVersions) { $psVersions += ", $vData" }
          else { $psVersions += $vData }
         }
       }
       Write-Output "$Computer : PowerShell $psVersions"
      }
     }
    }
  }
  $ErrorActionPreference = $OldErrorActionPreference
}

GET-PSVERSIONINFO "HAUSE","RM80","RM500","PE400","PE175"