Solved

Errors in powershell scripts for checking IE versions

Posted on 2014-10-05
6
404 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 78

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 78

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 68

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
Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

 
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 68

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

920 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

13 Experts available now in Live!

Get 1:1 Help Now