Solved

Errors in powershell scripts for checking IE versions

Posted on 2014-10-05
6
407 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 79

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 79

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 69

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
Does Powershell have you tied up in knots?

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

 
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 69

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

Does Powershell have you tied up in knots?

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 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