当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 在列表项目中查找孤立用户电子邮件

作者:精品下载站 日期:2024-12-14 14:08:46 浏览:14 分类:玩电脑

使用 PowerShell 在列表项目中查找孤立用户电子邮件


要求:
我们用于在组织范围内发送新闻通讯的定制应用程序将其发送电子邮件的用户列表保留在名为“订阅”的 SharePoint 列表中。用户在列表的人员选择器字段中进行配置。现在,当有人离开组织时,他们的帐户就会成为孤儿,他们的电子邮件也会失效。

因此,在处理电子邮件列之前,我们必须扫描列表中的人员选择器列以查找孤立用户电子邮件。

用于从电子邮件中扫描孤立用户的 PowerShell 脚本:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
 
$Web= Get-SPWeb "https://portal.crescent.com/News/"
$List = $Web.Lists["Subscriptions"]
$FieldName="Members"

foreach($item in $List.Items)
{
  if($Item[$FieldName] -ne $null)
  {
   #Get People picker field values collection
   $UserCollection = New-Object Microsoft.Sharepoint.SPFieldUserValueCollection($Web,$Item[$FieldName].ToString())
         
        #Get each User from the Person or Group field
        foreach($UserObj in $UserCollection)
        {
            #Try to get the user from AD from Email 
            $ADUser= Get-ADUser -Filter {mail -eq $UserObj.User.Email}
          
            #check if user email doesn't exist in AD
            if($ADUser -eq $null)
             {
                "https://portal.crescent.com/News/Pages/ViewUser.aspx?UserId=$($Item['ID'])" + $UserObj.User.LoginName + $UserObj.User.Email
             }
        }
    }
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯