2010-10-22 190 views
2

我是PowerShell的新手,我似乎無法找到無數谷歌搜索後如何解決這個問題。我知道這可能是容易的,但這裏基本上是我想要做的和錯誤顯示:PowerShell中的New-ItemProperty的小問題

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 

我清醒地認識到錯誤,這是顯而易見的。但我不知道正確的方法/命令來修復它...

回答

1

試試這個:

$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我沒有看到一個名爲「屬性」值的任何REGKEYS也許你可以匹配DriverDesc註冊價值?無論如何,你得到錯誤的原因是你必須指定PSPath到New-ItemProperty,即在腳本$key.PSPath中。

+0

'$ _。Property'是'NoteProperty System.String [] Property = System.String []'。所以這段代碼不能在2+屬性上工作。 – 2010-10-22 16:20:30

+0

啊,我明白了,你已經修好了。 – 2010-10-22 16:26:16