Windows PowerShell按数据范围过滤

时间:2018-04-16 19:02:24

标签: powershell okta okta-api

我有一个PowerShell脚本来从我们的SSO应用程序中取消已停用的帐户,但希望将其过滤到仅超过90天前停用的帐户。 然后我有另一个脚本来获取结果并从SSO应用程序中删除这些用户。

您能否告诉我如何在以下脚本中添加过滤器以排除结果,因为StatusChanged日期从当前日期开始大于90天。

$users = oktaListDeprovisionedUsers -oOrg PREV
$toexport = New-Object System.Collections.ArrayList

Foreach ($u in $users)
{
    $line = @{
              status = $u.status
              employeeid = $u.profile.employeeNumber
              firstName = $u.profile.firstName
              lastName = $u.profile.lastName
              email = $u.profile.email
              department = $u.profile.department
              supervisor = $u.profile.manager
              created = $u.created
              lastUpdated = $u.lastUpdated
              login = $u.profile.login
              title = $u.profile.title
              GroupName = $u.profile.Group_Name
              Organization = $u.profile.organization
              Location = $u.profile.workday_location
              User_type = $u.profile.userType
              StatusChanged = $u.StatusChanged


             }
    $obj = New-Object psobject -Property $line
    $_c = $toexport.Add($obj)
}
#Path for utility will have to be changed to a more generic location.
$toexport | Select-Object "login", "StatusChanged", "employeeid", "firstName","lastName", "email", "title","supervisor","department","Organization","Location", "GroupName" | >Export-Csv -Path "C:\OktaExport\user-list.csv" -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

您可以通过Where-Object

过滤$ users对象
$users = $users | Where-Object{((Get-Date) - $_.StatusChanged).TotalDays -gt 90}

将其添加到脚本的第二行。