Solved

Errors in powershell scripts for checking IE versions

Posted on 2014-10-05
6
400 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
Comment Utility
##############################################################################
##
## 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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
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
Comment Utility
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
Comment Utility
didn't see that line lol
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

744 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

16 Experts available now in Live!

Get 1:1 Help Now