PowerShell 中 New-ItemProperty 的小问题

发布于 2024-09-28 18:49:04 字数 1518 浏览 0 评论 0原文

我是 PowerShell 的新手,经过无数次 Google 搜索后,我似乎找不到如何解决这个问题。我知道这可能很容易,但这基本上就是我想要做的以及显示的错误:

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'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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2024-10-05 18:49:04

试试这个:

$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" 
    } 
}

顺便说一句,我没有看到任何名为“Property”的值的注册表项,也许您可​​以匹配 DriverDesc 注册表值?无论如何,您收到错误的原因是您必须在脚本 $key.PSPath 中指定 New-ItemProperty 的 PSPath。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文