在powershell中,可以得到RegistryKeys如下陣列:
$hkeys = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
當我檢查這個陣列的第一個元素時,這就是我得到的:
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 看起來有點奇怪,所以我進一步研究了一下:
> $hkeys[0].property.gettype
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String[] System.Array
property屬性中的元素,由于用冒號分隔,:看起來不像字串,所以我再仔細看了一下,發現它們確實是String物件:
> $hkeys[0].property[0].gettype
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
由于它們似乎是字串物件,因此我嘗試回應第一個物件。但是,它只顯示字串的第一部分,而不顯示冒號后面的部分:
> $hkeys[0].property[0]
DisplayName
我覺得這里有一些我不明白的基本原理。陣列的元素真的是String物件嗎?如果是這樣,為什么冒號后面的部分不會出現?
uj5u.com熱心網友回復:
注冊表物件具有定義的輸出格式,當沒有給出格式時,powershell 會使用該格式。你可以在這里閱讀更多about_Format.ps1xml
您可以通過呼叫來測驗
$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
注冊表的默認格式檔案(例如:C:\Windows\System32\WindowsPowerShell\v1.0\Registry.format.ps1xml)將顯示如下屬性
$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
正如你所注意到的,輸出是一個 string[]
要在 powershell 中獲取實際值,您需要呼叫一個方法或使用 Get-ItemProperty
$hkeys[0].getvalue('DisplayName') #you have to specify the property name
# or
$hkeys[0] | Get-ItemProperty
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364766.html
