Solved

Powershell scripting question

Posted on 2011-03-14
5
600 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

828 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