?
Solved

Errors in powershell scripts for checking IE versions

Posted on 2014-10-05
6
Medium Priority
?
457 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 82

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 82

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 1500 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

770 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