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

[玩转系统] 使用 PowerShell 在 SharePoint InfoPath 表单中查找和替换用户 ID

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

使用 PowerShell 在 SharePoint InfoPath 表单中查找和替换用户 ID


要求:替换 InfoPath 表单中的用户 ID。

一点背景知识:特定用户的用户帐户在 Active Directory 中发生更改,在 SharePoint 中,我们运行 Move-SPUser cmdlet 来适应用户的 SAM 帐户名称更改。但是,许多 InfoPath 表单库都有大量带有旧用户 ID 的 InfoPath 表单。当然,Move-SPUser 不会影响 InfoPath Forms!

[玩转系统] 使用 PowerShell 在 SharePoint InfoPath 表单中查找和替换用户 ID

用于在 InfoPath XML 表单中搜索和替换用户 ID 的 PowerShell 脚本:

此 PowerShell 脚本迭代给定网站集中的所有 InfoPath 表单、扫描并替换所有表单中的给定用户 ID。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$SiteCollURL="https://intranet.crescent.com"

#Old and New User IDs - CASE SENSITIVE!
$OldUserID="<pc:AccountId>i:0#.w|Crescent\JohnA</pc:AccountId>"
$NewUserID="<pc:AccountId>i:0#.w|Crescent\JonhAbraham</pc:AccountId>"

#Get all webs in the site collection
$WebsColl = Get-SPSite $SiteCollURL | Get-SPWeb -Limit All

#Iterate through each web
foreach($web in $WebsColl)
{
    Write-host "Processing web:"$web.Url
    
    #Get all InfoPath Form Libraries in the web
    $InfoPathLibs = $web.lists | where { $_.BaseType -eq "DocumentLibrary" -and $_.BaseTemplate -eq "XMLForm"} 

    #Loop through each InfoPath form library and Forms (.xml)
    Foreach($Library in $InfoPathLibs)
    {
        Foreach($Item in $Library.Items)
        {
            # Load the contents of the InfoPath Form
            $File = $Item.File
            $Data = [System.Text.Encoding]::ASCII.GetString($File.OpenBinary())
            
            #Check if the File has Old ID
            if($data.Contains($OldUserID))
            {
                $Data = $Data.Replace($OldUserID, $NewUserID)
                $Data = $Data.Replace("???","") #special characters fix!
                $File.SaveBinary([System.Text.Encoding]::ASCII.GetBytes($Data))

                Write-host "InfoPath XML File updated at $($web.URL)/$($Item.File.URL)"
            }
        }
    }
}

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

取消回复欢迎 发表评论:

关灯