Solved

Errors in powershell scripts for checking IE versions

Posted on 2014-10-05
6
438 Views
Last Modified: 2014-10-19
Hi folks,

I am write  script for checks IE versions on workstations of OU:


Open in new window

$array =@()
$computernames = Get-ADComputer -Filter * | where {$_.distinguishedName -like "*OU=w7,OU=auto,dc=contoso,dc=com*" } | select-object -expa Name

foreach ($server in $computernames)
{
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
$key = $reg.OpenSubkey("SOFTWARE\Microsoft\Internet Explorer")
$value = $key.GetValue('svcUpdateVersion')
 $obj = New-Object PSObject          
        $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $server          
        $obj | Add-Member -MemberType NoteProperty -Name "IEVersion" -Value $value  
        $array += $obj  
}  
$array | select ComputerName,IEVersion | export-csv d:\IE_Version2.csv

Open in new window


I am receive following error messages:

 

Open in new window

    Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
"
At C:\PowerShell\Scripts\5-october.ps1:6 char:1
+ $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException

You cannot call a method on a null-valued expression.
At C:\PowerShell\Scripts\5-october.ps1:7 char:1
+ $key = $reg.OpenSubkey("SOFTWARE\Microsoft\Internet Explorer")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\PowerShell\Scripts\5-october.ps1:8 char:1
+ $value = $key.GetValue('svcUpdateVersion')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
"
At C:\PowerShell\Scripts\5-october.ps1:6 char:1
+ $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

Open in new window

           

any help apprecated
0
Comment
Question by:nesher13
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40362203
##############################################################################
##
## Get-RemoteRegistryKeyProperty
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Get the value of a remote registry key property

.EXAMPLE

PS >$registryPath =
     "HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
PS >Get-RemoteRegistryKeyProperty LEE-DESK $registryPath ExecutionPolicy

#>

param(
    ## The computer that you wish to connect to
    [Parameter(Mandatory = $true)]
    $ComputerName,

    ## The path to the registry item to retrieve
    [Parameter(Mandatory = $true)]
    $Path,

    ## The specific property to retrieve
    $Property = "*"
)

Set-StrictMode -Version Latest

## Validate and extract out the registry key
if($path -match "^HKLM:\\(.*)")
{
    $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
        "LocalMachine", $computername)
}
elseif($path -match "^HKCU:\\(.*)")
{
    $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
        "CurrentUser", $computername)
}
else
{
    Write-Error ("Please specify a fully-qualified registry path " +
        "(i.e.: HKLM:\Software) of the registry key to open.")
    return
}

## Open the key
$key = $baseKey.OpenSubKey($matches[1])
$returnObject = New-Object PsObject

## Go through each of the properties in the key
foreach($keyProperty in $key.GetValueNames())
{
    ## If the property matches the search term, add it as a
    ## property to the output
    if($keyProperty -like $property)
    {
        $returnObject |
            Add-Member NoteProperty $keyProperty $key.GetValue($keyProperty)
    }
}

## Return the resulting object
$returnObject

## Close the key and base keys
$key.Close()
$baseKey.Close()

Open in new window

Example:
$reg = "hklm:\software\microsoft\Internet Explorer\"
.\Get-RemoteRegistryKeyProperty.ps1 mycomputtername $reg

which on my machine resulted in :

Version           : 9.11.9841.0
svcKBFWLink       : http://go.microsoft.com/fwlink/?LinkId=393949
svcVersion        : 11.0.9841.0
svcUpdateVersion  : 11.0.8
MkEnabled         : Yes
Build             : 99841
W2kVersion        : 9.11.9841.0
IntegratedBrowser : 1
svcKBNumber       : KB2953522
0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40362206
Your Code implementing my code:
$array =@()
$computernames = Get-ADComputer -Filter * | where {$_.distinguishedName -like "*OU=w7,OU=auto,dc=contoso,dc=com*" } | select-object -expa Name

foreach ($server in $computernames)
{
$reg = "hklm:\software\microsoft\Internet Explorer\"
$key = .\Get-RemoteRegistryKeyProperty.ps1 $server $reg
$value = $key.GetValue('svcUpdateVersion')
 $obj = New-Object PSObject          
        $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $server          
        $obj | Add-Member -MemberType NoteProperty -Name "IEVersion" -Value $value  
        $array += $obj  
}  
$array | select ComputerName,IEVersion | export-csv d:\IE_Version2.csv 

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40362321
nesher13, that code looks pretty much like written by becraig. Is that the case?
Anyway, it should be cleaned up. But what you really want to do is to check for the machine being reachable before trying to read its registry.
Get-ADComputer -Filter * |
  where {$_.distinguishedName -like "*OU=w7,OU=auto,dc=contoso,dc=com*" } |
  select-object -expand Name |
  % {
    if (Test-Connection -count 1 -TimeToLive 100 $_)
    {
      $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_) 
      $key = $reg.OpenSubkey("SOFTWARE\Microsoft\Internet Explorer") 
      $value = $key.GetValue('svcUpdateVersion')
    } else {
      $value = $null
    }
    New-Object PSObject -Property @{
      ComputerName = $_
      IEVersion    = $value  
    }
  } |
  select ComputerName,IEVersion | export-csv -NTI d:\IE_Version2.csv

Open in new window

The CSV lists all PCs, and if offline the version string is empty.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40362595
I think all you have to do just change the $server to $server.name.

$server in get-adcomputer will return the result of all the info but not a name.

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $($server.name)) 

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40362626
Justin, that is what the -Expand in Select-Object is for: The result is a string instead of an object with a name property.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40362638
didn't see that line lol
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

729 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