Solved

Powershell scripting question

Posted on 2011-03-14
5
596 Views
Last Modified: 2012-06-27
I was writing a little script to tell which versions of .net framework are installed.  In powershell I wrote the following but am getting an error stating that there is nothing to match with the -eq operator.  I am new to powershell.  What am I doing wrong?

$NETv4 = "hklm:\software\microsoft\Net Framework Setup\NDP\v4"
$NETv35 = "hklm:\software\microsoft\Net Framework Setup\NDP\v3.5"
$NETv30 = "hklm:\software\microsoft\Net Framework Setup\NDP\v3.0"
$NETv20 = "hklm:\software\microsoft\Net Framework Setup\NDP\v2.0"
$NETv11 = "hklm:\software\microsoft\Net Framework Setup\NDP\v1.1.4322"


IF (Test-Path -Path $NETv4 -eq $True) {Write-Host ".Net Framework Version 4 is installed"}
IF (Test-Path -Path $NETv35 -eq $true) {Write-Host ".Net Framework Version 3.5 is installed"}
IF (Test-Path -Path $NETv30 -eq $true) {Write-Host ".Net Framework Version 3.0 is installed"}
IF (Test-Path -Path $NETv20 -eq $true) {Write-Host ".Net Framework Version 2.0 is installed"}
IF (Test-Path -Path $NETv11 -eq $true) {Write-Host ".Net Framework Version 1.1 is installed"}

Is there a way to either loop the values through or use an array so I don't have to write a different line for each version?
0
Comment
Question by:childersj
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
MarkXIII earned 400 total points
ID: 35129231
Hi Childersj,
You do not need the $true variable.
Just test like this:
IF (Test-Path -Path $NETv4) {Write-Host ".Net Framework Version 4 is installed"}

And it will work fine.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35129526
Try this one:
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
 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 100 total points
ID: 35129555
Actually in your original code the parentheses are missing:
IF ((Test-Path -Path $NETv4) -eq $True) {Write-Host ".Net Framework Version 4 is installed"}

But as MarkXIII mentioned, you can omit the "-eq $true".
0
 

Author Closing Comment

by:childersj
ID: 35131788
Thanks!
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35132870
Have you tried the code in my first comment? It's a solution without enumerating the current .net framework versions, and still gives all that iare installed.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now