views:

25

answers:

1

I'm new to PowerShell and I can't seem to find how to fix this after countless Google searches. I know it's probably easy, but here's basically what I want to do and the error that shows:

PS C:\Windows\system32> $path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"

Get-Childitem $path -ErrorAction SilentlyContinue | Foreach {
    $key = Get-Item $_.PSPath

    if($key.Property -eq "VMnet") {
        New-ItemProperty $key -name "*NdisDeviceType" -value "1"
    }
}
New-ItemProperty : Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0014' because it does not exist.
At line:7 char:25
+         New-ItemProperty <<<<  $key -name "*NdisDeviceType" -value "1"
    + CategoryInfo          : ObjectNotFound: (C:\Windows\syst...02BE10318}\0014:String) [New-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand

New-ItemProperty : Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0015' because it does not exist.
At line:7 char:25
+         New-ItemProperty <<<<  $key -name "*NdisDeviceType" -value "1"
    + CategoryInfo          : ObjectNotFound: (C:\Windows\syst...02BE10318}\0015:String) [New-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand

I clearly understand the error, it's obvious. But I don't know the proper way/command to fix it...

+1  A: 

Try this:

$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\" +
        "{4D36E972-E325-11CE-BFC1-08002BE10318}" 

Get-Childitem $path -ErrorAction SilentlyContinue | 
    Where {(Get-ItemProperty $_.PSPath DriverDesc) -match 'VMnet' } |
    Foreach { 
        New-ItemProperty $_.PSPath -name "*NdisDeviceType" -value "1" 
    } 
}

BTW I don't see any regkeys for values named "Property" perhaps you could match on the DriverDesc reg value? Anyway, the reason you're getting the error is you have to specify the PSPath to New-ItemProperty ie in your script $key.PSPath.

Keith Hill
@Keith: `$_.Property` is `NoteProperty System.String[] Property=System.String[]`. So that this code will not work on 2+ properties.
Roman Kuzmin
Ah, I see, you have already fixed it.
Roman Kuzmin