Solved

Powershell scripting question

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dropbox in Windows Server 2008 4 32
Script to change all permissions to my user 7 35
Xenapp 6.5 Powershell question 9 19
Cleaning up a desktop after leaving a domain 3 27
Synchronize a new Active Directory domain with an existing Office 365 tenant
OfficeMate Freezes on login or does not load after login credentials are input.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

832 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