我们的sitecore系统中有超过20万名用户。我需要导出满足特定条件的用户列表。我为此使用Powershell脚本,并且使用get-user命令检索用户。然后,我遍历用户列表并选择符合我的条件的用户;在我的情况下,那些年满18岁的用户。然后使用export-csv将结果写到csv文件中。我发现这需要1.5个小时才能完成。
我的问题是,有什么方法可以吸引用户并指定我的18岁以上年龄标准?字段年龄存储在自定义属性中。另外,还有其他有效的方法(除了Powershell之外)可以完成我在这里要做的事情?
这是原始代码:
function export($user)
{
$age = $user.profile.GetCustomProperty("age")
if{$age -gt 18)
{
$id = $user.profile.GetCustomProperty("id")
$firstname = $user.profile.GetCustomProperty("first name")
$user | select-object -property @{Name="First Name";Expression={$firstname}},
@{Name="Age";Expression={$age}},
@{Name="ID";Expression={$id}} |
Export-CSV -Path c:\temp\out.csv -Append -NoTypeInformation
}
}
$users = get-user -Filter *
if($users -ne $null)
{
$users | foreach {export($_)}
}
最佳答案
更新:
根据您的示例,我可以了解为什么要花这么长时间。您正在为每次迭代导出到CSV。
试试这个:
$users = Get-User -Filter * | Where-Object { $_.Profile.GetCustomProperty("age") -gt 18 }
$property = @(
"Name",
@{Name="First Name";Expression={$PSItem.Profile.GetCustomProperty("first name")}},
@{Name="Age";Expression={$PSItem.Profile.GetCustomProperty("age")}},
@{Name="ID";Expression={$PSItem.Profile.GetCustomProperty("id")}}
)
$users | Select-Object -Property $property | Export-CSV -Path c:\temp\out.csv -Append -NoTypeInformation
旧评论:
我看得越多,我就怀疑这是否可以做到。 age属性应该被序列化并存储在配置文件中。除非有一种更快的方法来提取个人资料日期,否则我不确定还能采取什么其他措施来加快进度。
我怀疑您正在执行以下操作:
Get-User -Filter * | Where-Object { $_.Profile.GetCustomProperty("Age") -gt 18 }
我不知道比这更快的方法。
关于powershell - Sitecore Powershell get-user命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34982451/