Test PS and ARS version numbers

Here’s a script I found on the internet, and modified to show me not only the PowerShell version numbers but the ARS version details – if installed that is….

You can use this in any script that needs to check the version numbers meet the minimum requirements by accessing the objects parameters and comparing version numbers.

I just updated the script to list powershell version 3 12/2/2015 – better late than never I guess not that powershell 4 is around 🙂

cls
function Get-PSVersion {
<# 
 .SYNOPSIS 
 Returns a custom PS Object listing the PowerShell and ARS versions
.DESCRIPTION 
 Returns a custom PS Object listing the PowerShell and ARS versions 
 .PARAMETER Credential
 Optional credentials that can be used to connect to the remote computer 
 .PARAMETER ComputerName 
 A array of computers to connect to and collect the version information 
 .INPUTS 
 Array of computer Names 
 [System.Management.Automation.PSCredential] 
 .OUTPUTS 
 Custom PS Object 
 ComputerName 
 PSBuildVersion 
 PSVersion 
 OSName 
 PowerGUIVersion 
 QuestCmdLetVersion  ARSVersion 
 .EXAMPLE 
"localhost" | Get-PSVersion 
 Using the pipeline to pass the list of computer names to the function 
 .EXAMPLE 
 Get-PSVersion "localhost" 
.EXAMPLE 
 Get-PSVersion  Calling the function with no parameters connects to the local host. 
 .NOTES 
 Version : 1.0 DATE HERE Author : Lee Andrews
.LINK 
https://clan8blog.wordpress.com/ 
 #>

[CmdletBinding()] 
 param(
 [parameter(Position=0,ValueFromPipeline=$true)][ValidateNotNullOrEmpty()]
 [String[]]$ComputerName = @('.'),
 [Parameter()][ValidateNotNull()] [System.Management.Automation.Credential()] 
 $Credential = [System.Management.Automation.PSCredential]::Empty
 )

process { 
 if (Test-Connection -ComputerName $computername -Count 1 -ErrorAction SilentlyContinue) { 
  $WMIcommonParameters = @{
   Namespace="root\CIMV2" 
   ComputerName=$computername 
   Credential=$credential 
   ErrorAction="SilentlyContinue"  
  } 
  # get the ARS version details
  try { $products = Get-WmiObject  @WMIcommonParameters -class Win32_Product }
  catch { } 
  if ( $products ) { 
   $ADCmdLetVersion = "Not Found" 
   $PowerGUIVersion = "Not Found" 
   $ARSVersion = "Not Found"
   ForEach ( $product in $products ) { 
    switch -wildcard ( $product.name ) {
     "Quest ActiveRoles Management Shell for Active Directory*" { 
      $ADCmdLetVersion = $product.version
      Break
     }
     "Quest One ActiveRoles Management Shell for Active Directory*" {
      $ADCmdLetVersion = $product.version
      Break
     }
     "Quest PowerGUI*" { 
      $PowerGUIVersion = $product.version 
      Break
     }
     "Quest ActiveRoles Server*" { 
      $ARSVersion = $product.version 
      Break
     }
     "Quest One ActiveRoles*" { 
      $ARSVersion = $product.version 
      Break
     }
    } # switch -wildcard ( $product.name )
   } # ForEach ( $product in $products )
  } # if ($product) 
  else {
   $ADCmdLetVersion = "Unable connect to WMI"
   $PowerGUIVersion = "Unable connect to WMI"
   $ARSVersion = "Unable connect to WMI"
  } # else if ($product) 
  try { $OS = Get-WmiObject @WMIcommonParameters -Class Win32_OperatingSystem }
  catch { } 
  if ($OS) { 
   $path  = "$($OS.SystemDirectory -replace '\\','\\')\\WindowsPowerShell\\v1.0\\powershell.exe"
   $OSName = $OS.Name.Split('|')[0]
  } # if ($OS) { 
  else { $OSName = "Unable to connect to WMI" } 
  # now get the PS Version
  $query = "SELECT Version FROM CIM_DataFile WHERE Name = '$path'"
  try { $PSEXE = Get-WmiObject -Query $query -ComputerName $computername -Credential $credential } 
  catch { } 
  if ( ( $PSEXE ) -and ( $PSEXE.Version ) ) { 
   $buildversion = $PSEXE.Version.Split()[0]
   $versionPresent = [version]$buildversion 
   $versionRequired = [version]'6.0.6002.18111'
   $version3 = [version]'6.2.9200.16398'
   if ($versionPresent -ge $versionRequired) { 
    $psversion = "V2 RTM"
    if ( $versionPresent -eq $version3 ) {
     $psversion = "V3"
    }
   }
   elseif ($versionPresent.Major -ge 6) {$psversion = "V2 CTP Prerelease – Update to V2 RTM!"}
   else {$psversion = "V1"}
  } 
  else { 
   $psversion = "Unable to Detect or Not Installed" 
   $BuildVersion=[version]$null 
   $Version='n/a' 
   $Description='Unable to connect to computer – ping fail to connect to WMI'
  } # else if ($PSEXE.Version) {
 } # if (Test-Connection
 else { 
  $ADCmdLetVersion = "Unable to connect to computer – ping fail" 
  $PowerGUIVersion = "Unable to connect to computer – ping fail"
  $ARSVersion = "Unable to connect to computer – ping fail" 
  $OSName ="Unable to connect to computer – ping fail" 
  $psversion = "Unable to connect to computer – ping fail" 
  $BuildVersion=[version]$null
  $Version='Unable to connect to computer – ping fail' } 
  $Properties = @{
   ComputerName=$computername[0]
   PSBuildVersion=$buildversion
   PSVersion=$psversion
   OSName=$OSName
   PowerGUIVersion = $PowerGUIVersion
   QuestCmdLetVersion = $ADCmdLetVersion
   ARSVersion = $ARSVersion
  } 
  New-Object PSObject -Property $($Properties | Sort-Object)
 } # end process 
} # end function

Get-PSVersion "localhost" 
Get-PSVersion "serverName2", "ServerName1" | Get-PSVersion

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.