使用powershell

时间:2015-10-27 11:18:17

标签: powershell sharepoint csom

我已通过Sharepoint管理员门户创建了一个安全组,我们将其命名为" group1 "。我有一个网站,有一个列表,在该列表中有一个人/组列字段,我们将其命名为" PersonGroupColumn "。

当我在列表中添加新项目时,我可以找到" group1 "当我输入" PersongroupColumn"。虽然,我想要做的是设置一个 powershell脚本,用" group1 "更新列表中的所有项目。在" PersonGroupColumn "

这是我的开始脚本:

# Get Start Time
$startDTM = (Get-Date)

# Global variables
$User = ""
$Pwd = ""
$SiteURL = ""

# Functions

function addUser
{
    param([string]$DisplayName,[string]$Group)
    $d=New-Object PSObject
    $d | Add-Member -Name DisplayName -MemberType NoteProperty -Value $DisplayName
    $d | Add-Member -Name Group -MemberType NoteProperty -Value $Group
    return $d
}

#Libraries
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint.Client') > $null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint') > $null
[System.Reflection.Assembly]::LoadWithPartialName('MsOnline') > $null

# Password Init
$Password = ConvertTo-SecureString $Pwd -AsPlainText -Force

# Connection to Office 365
$Creds = New-Object System.Management.Automation.PSCredential($User, $Password)

connect-msolservice -credential $Creds


# Gathering groups and members
$array = @()
$Groups = Get-MsolGroup
foreach($Group in $Groups)
{
    $Members = Get-MsolGroupMember -GroupObjectId $Group.ObjectId

    $line = New-Object psobject
    foreach($Member in $Members)
    {
        $array += AddUser $Member.DisplayName $Group.DisplayName
    }
}

# Connection to sharepoint Online
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $Password)
$Context.Credentials = $Creds

# gathering the list
$Web = $Context.Web
$List = $Web.Lists.GetByTitle('Rapport de visite')
$Context.Load($Web)
$Context.Load($List)
$Context.ExecuteQuery()

$Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$Items = $List.GetItems($Query)
$Context.Load($Items)

foreach($Item in $Items)
{

    $AffectedGroup = ""
    $Item['Groupes'] = ""

    foreach($Member in $array)
    {
        if($Item['Author'].LookupValue -eq $Member.DisplayName) {
            $AffectedGroup = $Member.Group
        }
    }

    if($Item["Groupes"] -ne $AffectedGroup) {
        $Item['Groupes'] = $AffectedGroup + ";"
        $Item.Update()
        $Context.ExecuteQuery()
    }

}

# Get End Time
$endDTM = (Get-Date)

# Echo Time elapsed
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"

似乎我需要创建一个 fieldUserValue 对象并将其推送到项目的字段中,但我需要该组的 LookupId 而且我不需要#39;知道从哪里得到它。

非常感谢一些帮助。

谢谢!

0 个答案:

没有答案