如何访问Get-Child注册表输出的属性

问题描述

使用以下代码,我填充了NameLastlogon,但没有填充ProfilePath

Add-RegKeyMemberhttps://gallery.technet.microsoft.com/scriptcenter/Get-Last-Write-Time-and-06dcf3fb

我尝试使用ProfileImagePath$Profile.Properties.ProfileImagePath等访问$Profile.Name.ProfileImagePath,但是它们都返回空白(可以为空)。这个看似对象如何使这些属性可用?

$Profiles = get-childitem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Add-RegKeyMember

foreach($Profile in $Profiles)
{

  $ThisProfileInfo = @{Name=$Profile.Name;
                     Lastlogon=$Profile.LastWriteTime;
                     ProfilePath=$Profile.ProfileImagePath}
  $Profile
}

Name                           Property                                                                                                                                                       
----                           --------                                                                                                                                                       
S-1-5-18                       Flags            : 12                                                                                                                                          
                               ProfileImagePath : C:\WINDOWS\system32\config\systemprofile                                                                                                    
                               RefCount         : 1                                                                                                                                           
                               Sid              : {1,1,0...}                                                                                                                             
                               State            : 0

解决方法

这是因为[Microsoft.Win32.RegistryKey]对象将属性返回为字符串数组。您只需从对象本身检索值ProfileImagePath即可:

ProfilePath=$Profile.GetValue("ProfileImagePath")
,

请查看以下对脚本的调整。

您可以使用方法GetValue拉出属性的子值。

我还调整了您存储每次迭代的方式,并在foreach循环后输出值,如上例所示,将只输出每个$profile,就像循环前一样。

我尚未使用Add-RegKeyMember进行测试,因此无法确认是否将拉动LastWriteTime属性,但是我可以确认这将拉动profileimagepath属性。

$Profiles = get-childitem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Add-RegKeyMember

$ProfileData = @()

foreach($Profile in $Profiles){
    $ThisProfileInfo = $null

    $ThisProfileInfo = @{Name=$Profile.Name;
                 LastLogon=$Profile.GetValue("LastWriteTime");
                 ProfilePath=$Profile.GetValue("ProfileImagePath")}

    $ProfileData += $ThisProfileInfo
}

$ProfileData

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...