问题描述
使用以下代码,我填充了Name
和Lastlogon
,但没有填充ProfilePath
。
Add-RegKeyMember
是https://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