• Status: Open
  • Priority: High
  • Security: Public
  • Views: 46
  • Last Modified:

PowerShell script Get-ItemProperty issue on Windows 10 (NOT ON Windows 7) - Registry issue

Dear,
I have this function which is running fine on Windows 7 BUT Does NOT on Windows 10.

The parameter used is  HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds\{4CC1FD56-6978-41A1-B787-484B2C6FEGF2}"

The error got on WINDOWS 10 ONLY
Get-ItemProperty : Cannot find path
'C:\THESIS\HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds\{4CC1FD56-6978-41A1-B787-484B2C6FEGF2}' because it does not exist.

so the issue is that the C:\THESIS IS ADDED automatically.... (not in Windows 7). any idea how to solve this?

Code is below :

function CheckForConnection
{
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]
        $URL
    )
 
    [string] $connectionKey = "" 
    [bool] $found = $false
#    foreach ($connectionKey in get-item 'HKCU:\Software\Microsoft\Workspaces\Feeds\*' 2> $null)  
    foreach ($connectionKey in get-item 'HKCU:\Software\Microsoft\Workspaces\Feeds\*')  
    {
        Write-Host "URL searched is $URL"
      Write-Host "Connection Key is  $connectionKey"

        if ( ($connectionKey | Get-ItemProperty -Name URL).URL -eq $URL)
        {
            $found = $true
            break
        }
    }
 
    return $found
}
0
François VDC
Asked:
François VDC
5 Comments
 
McKnifeCommented:
Win10 does not care about c:\thesis. Are you doing something differently without knowing? Are you starting the script from a different directory as compared to win7?
0
 
François VDCIcT service DeskAuthor Commented:
no.
nothing different. It's clearly a difference between Windows 7 and Windows 10.
0
 
oBdACommented:
The difference is that you never bothered to update the Powershell on your Windows 7 - it's still on v2.0 from 2009. The script will fail on v3.0 and later.
The issue is the line
[string] $connectionKey = ""

Open in new window

With the [string] at the beginning of the variable you're assigning, you're "strongly typing" it, that is, it you say "This is a string, and never allow it to be anything else!" (at least the 'never' is true for PS 3.0 and later).
Don't do that. Unless you really, really know what you're doing, and that does not seem to be the case. Let Powershell take care of the variable types. Don't try to fix what's not broken.
Because what's happening in PS 3.0 and later in the ForEach loop is that the registry items are immediately force-casted to strings when assigning $connectionKey (because you told it to do so).
And when a registry item is casted to a string, the good old path ("HKEY_CURRENT_USER\Software\...") is returned, not the one that Get-itemProperty can handle ("HKCU:\Software" or "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software"). Since "HKEY_CURRENT_USER\Software\..." is only a partial path, PS adds the current location ('C:\Thesis').
If you hadn't strongly typed $connectionKey, it would remain a registry item, would be piped to Get-ItemProperty, which would analyze it, and find and use the PSPath property.

To fix it, simply remove the above line completely.
And while you're at it, remove the [bool] in front of the next line as well.
$found = $false  will work just fine all by itself.
0
 
QlemoC++ DeveloperCommented:
Sorry, oBdA, but you explanation about the difference of W7 and W10 is wrong (just tested this moment). PS 2 handles it exactly the same as PS 5.1 - a strict string cast.
The string cast caused by strong typing is wrong here, that much is true, as the explanation why.

François, you might have been lucky or  in a different location on W7 to get the script working. I cannot see any way allowing successful execution, though.
0
 
oBdACommented:
Paste that into a PS console (PS 3.0 or later, obviously).
Line 2 will result in the expected error.
Line 3 will return the registry item.
$sb = {Write-Host -Fore Black -Back Yellow "Powershell v$($PSVersionTable.PSVersion.ToString())"; Remove-Variable connectionKey -ErrorAction SilentlyContinue; [string] $connectionKey = ""; foreach ($connectionKey in get-item 'HKCU:\Software') {$connectionKey | Get-ItemProperty}}
Invoke-Command $sb
powershell.exe -Version 2 -Command $sb

Open in new window

I rest my case.
0

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now