Sont RegistryKey propriétés vraiment des objets String en powershell?

0

La question

Dans powershell, il est possible d'obtenir un tableau de RegistryKeys comme suit:

$hkeys = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

Quand j'ai inspecter le premier élément de ce tableau, c'est ce que j'obtiens:

    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall


Name                           Property                                                                                                                    
----                           --------                                                                                                                    
7-Zip                          DisplayName     : 7-Zip 21.03 beta (x64)                                                                                    
                               DisplayVersion  : 21.03 beta                                                                                                
                               DisplayIcon     : C:\Program Files\7-Zip\7zFM.exe                                                                           
                               InstallLocation : C:\Program Files\7-Zip\                                                                                   
                               UninstallString : "C:\Program Files\7-Zip\Uninstall.exe"                                                                    
                               NoModify        : 1                                                                                                         
                               NoRepair        : 1                                                                                                         
                               EstimatedSize   : 5237                                                                                                      
                               VersionMajor    : 21                                                                                                        
                               VersionMinor    : 3                                                                                                         
                               Publisher       : Igor Pavlov                                                                                               

Property semblait un peu étrange, j'ai donc cherché plus loin encore dans ce:

> $hkeys[0].property.gettype

IsPublic IsSerial Name                                     BaseType                                                                                        
-------- -------- ----                                     --------                                                                                        
True     True     String[]                                 System.Array                                                                                    

Les éléments de la property attribut, car ils sont délimités par une virgule : ne semble pas comme des cordes, j'ai donc cherché un peu plus loin, mais a trouvé qu'ils sont en effet String objets:

> $hkeys[0].property[0].gettype

IsPublic IsSerial Name                                     BaseType                                                                                        
-------- -------- ----                                     --------                                                                                        
True     True     String                                   System.Object                                                                                   

Depuis, ils semblent être des objets string, j'ai essayé de faire écho à la première. Cependant, il ne montre que la première partie de la chaîne et non pas la partie après la virgule:

> $hkeys[0].property[0]
DisplayName

Je se sentir comme il ya quelque chose de fondamental dont je ne comprends pas ici. Sont les éléments du tableau vraiment String les objets? Si oui, pourquoi ne pas la partie après les deux points apparaissent?

arrays object powershell registry
2021-11-23 17:08:14
1

La meilleure réponse

1

Les objets de registre est définie pour le format de sortie qui powershell utilise pas quand le format est donné. Vous pouvez en lire plus ici about_Format.ps1xml

Vous pouvez tester cela en appelant

$hkeys #formated with name:value, actually uses $hkeys | Out-Default

$hkeys | Format-Table Property #value won't show anymore

$hkeys | Format-List #value won't show anymore

Le format par défaut du fichier de registre (ex: C:\Windows\System32\WindowsPowerShell\v1.0\Registry.format.ps1xml) permet d'afficher la Propriété comme suit

$result = (Get-ItemProperty -LiteralPath $_.PSPath |
    Select * -Exclude PSPath,PSParentPath,PSChildName,PSDrive,PsProvider |
    Format-List | Out-String | Sort).Trim()
$result = $result.Substring(0, [Math]::Min($result.Length, 5000) )
if($result.Length -eq 5000) { $result += "..." }
$result

et comme vous l'avez remarqué, la sortie est un string[]

Pour obtenir la valeur réelle dans powershell, vous devez appeler une méthode ou l'utilisation Get-ItemProperty

$hkeys[0].getvalue('DisplayName') #you have to specify the property name
# or
$hkeys[0] | Get-ItemProperty
2021-11-23 21:18:45

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................