开发者

Little issue with New-ItemProperty in PowerShell

开发者 https://www.devze.com 2023-01-20 19:17 出处:网络
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:

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\{4开发者_运维问答D36E972-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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消