Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to search in an array correctly in powershell?

Posted on 2011-03-14
4
Medium Priority
?
672 Views
Last Modified: 2012-05-11
I have been playing with a powershell script to locate what versions of .net framework are installed.  Partially because I am trying to pick up powershell so I'd rather not just google for a script that already exists.  I'd rather figure out the best way to accomplish and understand why.

So, what I am doing right now is:
$arrVersion = "v4","v3.5","v3.0","v2.0.50727","v1.1.4322"
$strNetRegPath = "hklm:\software\microsoft\net framework setup\NDP\"+$arrVersion
IF ((test-path -path $strNetRegPath) -eq $True){Write-Host "The version installed is "+ $arrVersion}

Now I know that this is wrong and I am missing things as this will obviously return all of the values in the array.  I just want it to match the correct one for each registry key in that path.  I'm not worried about the service pack level for now.  I'll get to working with the properties after I get this working.  What is the best way for me to make this work?
0
Comment
Question by:childersj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 35132422
Hello there :)

You need a loop for your array:
$arrVersion = "v4","v3.5","v3.0","v2.0.50727","v1.1.4322"
ForEach ($Version in $arrVersion) {
  $strNetRegPath = "hklm:\software\microsoft\net framework setup\NDP\$Version"

  If ((Test-Path -Path $strNetRegPath) -eq $True) {
    Write-Host "The version installed is $Version"
  }
}

Open in new window

So far so good, and there's nothing wrong with that, but it's perhaps not very PowerShelly. We could play with things, for a start, we might go with a pipeline approach. There are two parts to this, the first takes our If statement and makes it Where-Object, the second deals with the results.
$Versions = "v4","v3.5","v3.0","v2.0.50727","v1.1.4322"
$Versions | 
  Where-Object { Test-Path -Path "HKLM:\software\microsoft\net framework setup\NDP\$_" } |
  Select-Object @{n='InstalledVersions';e={ $_ }}

Open in new window

The loop is still there, kind of, but now it's implicit instead of explicit.

HTH

Chris
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 35132511
I just ran your script, Chris.  Much enjoyed.  I'll have to keep this one for the future.  
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35133316
Or without any arrays, this solution will work for future .NET FW versions:
dir "hklm:\software\microsoft\Net Framework Setup\NDP\v*" | %{  
    if($_.name -match "Net\sFramework\sSetup\\NDP\\v(\d\.\d)"){  
        ".Net Framework Version $($matches[1]) is installed"  
    }  
}

Open in new window

0
 

Author Closing Comment

by:childersj
ID: 35138287
Thanks for the help.  The loop is what I needed!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question