Sitecore powershell get-user 命令

Sitecore powershell get-user command

我们的 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($_)}
}

I am then looping through the list of users and picking those that meet my criteria

你不应该这样做。而是直接使用 Get-User

过滤用户
Get-User -Filter * -ResultSize Unlimited | Where-Object {$_.age -ge 18}

再举一个例子,我将过滤 18 岁以上且名字以 "Ste" 开头的用户。

Get-User -Filter * -ResultSize Unlimited | Where-Object {$_.age -ge 18 -and $_.FirstName -like "Ste*"}

更新:

根据您的示例,我明白了为什么需要这么长时间。您正在为每次迭代导出到 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

旧评论:

我越看越怀疑这是否可行。年龄 属性 应该序列化并存储在配置文件中。除非有更快的方法来提取个人资料日期,否则我不确定还能做些什么来加快速度。

我怀疑你在做这样的事情:

Get-User -Filter * | Where-Object { $_.Profile.GetCustomProperty("Age") -gt 18 }

我不知道还有比这更快的方法了。