2010-10-22 31 views
2

Je ne connais pas encore PowerShell et je n'arrive pas à trouver de solution après d'innombrables recherches sur Google. Je sais qu'il est probablement facile, mais voici essentiellement ce que je veux faire et l'erreur qui montre:Petit problème avec New-ItemProperty dans PowerShell

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 

Je comprends bien l'erreur, il est évident. Mais je ne connais pas le chemin/commande appropriée pour y remédier ...

Répondre

1

Essayez ceci:

$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 je ne vois pas de REGKEYS pour les valeurs nommées « propriété » vous pourriez peut-être correspondre à la valeur reg DriverDesc? Quoi qu'il en soit, la raison pour laquelle vous obtenez l'erreur est que vous devez spécifier le PSPath à New-ItemProperty, c'est-à-dire dans votre script $key.PSPath.

+0

'$ _. Property' est la propriété' NoteProperty System.String [] Property = System.String [] '. Pour que ce code ne fonctionne pas sur 2+ propriétés. –

+0

Ah, je vois, vous l'avez déjà réparé. –