Sono RegistryKey proprietà davvero Stringa di oggetti in powershell?

0

Domanda

In powershell, è possibile ottenere un array di RegistryKeys come segue:

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

Quando ho ispezionare il primo elemento di questo array, questo è quello che ottengo:

    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 sembrava un po ' strano, così ho guardato ulteriormente che:

> $hkeys[0].property.gettype

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

Gli elementi in property attributo, dal momento che sono delimitate da due punti : non sembra come corde, così ho guardato un po ' di più, ma ha scoperto che sono davvero String oggetti:

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

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

Dal momento che sembrava di essere oggetti string, ho provato a eco il primo. Tuttavia, si riporta solo la prima parte della stringa e non la parte dopo i due punti:

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

Sento che c'è qualcosa di fondamentale che non capisco qui. Sono gli elementi della matrice davvero String gli oggetti? Se è così, perché non parte dopo i due punti compaiono?

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

Migliore risposta

1

Oggetti del registro di sistema è definito un formato di output che powershell viene utilizzato quando non è formato data. Si può leggere di più qui about_Format.ps1xml

Puoi provare chiamando

$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

Il formato predefinito dei file di registro di sistema (es: C:\Windows\System32\WindowsPowerShell\v1.0\Registry.format.ps1xml viene visualizzata la Struttura come segue

$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

e come hai notato, l'output è un string[]

Per ottenere il valore effettivo in powershell è necessario chiamare un metodo o l'uso Get-ItemProperty

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

In altre lingue

Questa pagina è in altre lingue

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