使用下面的代码,我得到Name
&已填充LastLogon
,但未填充ProfilePath
。
Add-RegKeyMember
为https://gallery.technet.microsoft.com/scriptcenter/Get-Last-Write-Time-and-06dcf3fb。
我曾尝试使用$Profile.Properties.ProfileImagePath
、$Profile.Name.ProfileImagePath
和其他访问ProfileImagePath
,但它们都返回空白(可能为null(。这个看似物体的东西究竟是如何使这些特性可用的?
$Profiles = get-childitem "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionProfileList" | 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:WINDOWSsystem32configsystemprofile
RefCount : 1
Sid : {1, 1, 0, 0...}
State : 0
这是因为[Microsoft.W32.RegistryKey]对象以字符串数组的形式返回属性。您应该简单地从对象本身检索值ProfileImagePath:
ProfilePath=$Profile.GetValue("ProfileImagePath")
请参阅下面对脚本的调整。
可以使用方法GetValue
提取属性的子值。
我还调整了存储每次迭代并在foreach
循环后输出值的方式,因为上面的示例将只输出循环前的每个$profile
。
我还没有用Add-RegKeyMember
进行测试,因此我无法确认这是否会拉取LastWriteTime
属性,但我可以确认这将拉取profileimagepath
属性。
$Profiles = get-childitem "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionProfileList" | Add-RegKeyMember
$ProfileData = @()
foreach($Profile in $Profiles){
$ThisProfileInfo = $null
$ThisProfileInfo = @{Name=$Profile.Name;
LastLogon=$Profile.GetValue("LastWriteTime");
ProfilePath=$Profile.GetValue("ProfileImagePath")}
$ProfileData += $ThisProfileInfo
}
$ProfileData