使用Powershell在线自动在sharepoint中进行网站集管理员分配以进行备份

时间:2018-06-20 10:33:03

标签: powershell sharepoint office365 sharepoint-online

我被要求创建一个Powershell脚本来自动为SharePoint-Online中的新网站集管理员分配用于备份目的的NetApp CloudControl。

这是我第一次在PowerShell中工作,现在我被困住了,不知道再看哪里,或者至少不了解我在看什么。

该脚本应该执行以下操作:

  • 获取Microsoft租户和密码
  • 在已填写凭据的地方创建一个新的ps脚本
  • 连接到每个用户的共享点在线并查找个人空间(用于企业站点的一个驱动器)
  • 剪切日志并在写入200行以上的情况下创建第二个日志
  • 阅读日志,并将服务帐户设置为sitecollectionadmin
  • 创建任务以每周运行一次创建的脚本

目前,我可以这样做:

  • 获取Microsoft租户和密码
  • 保存凭据
  • 连接到每个用户的共享点在线并查找个人空间(用于企业站点的一个驱动器)
  • 阅读日志并将服务帐户设置为sitecollection管理员

任何人都可以帮助我进行下一步吗?

P.S。请原谅,我是整个发布脚本,只是不知道应该删掉什么。

$TenantName = $null0
$TenantPassword = $null1
if($TenantName -eq $null0){
    $TenantName = Read-Host "Enter Office 365 - Tenant Name."
    $NewScript = Get-Content $PSCommandPath | ForEach-Object {$_ -replace '^\$TenantName = \$null0$',"`$TenantName = '$TenantName'"}
    $NewScript | Out-File $PSCommandPath -Force
}
if($TenantPassword -eq $null1){
    $TenantPassword = Read-Host "Enter Password for netapp-service@$($TenantName).onmicrosoft.com."
    $NewScript = Get-Content $PSCommandPath | ForEach-Object {$_ -replace '^\$TenantPassword = \$null1$',"`$TenantPassword = '$TenantPassword'"}
    $NewScript | Out-File $PSCommandPath -Force
}
$username = "netapp-service@$($TenantName).onmicrosoft.com"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $TenantPassword -asplaintext -force)
Connect-SPOService -Url https://$($TenantName)-admin.sharepoint.com/ -Credential $cred
$AdminURI = "https://$($TenantName)-admin.sharepoint.com"
$AdminAccount = "netapp-service@$($TenantName).onmicrosoft.com"
$AdminPass = $TenantPassword
$eDiscoveryUser = "netapp-service@$($TenantName).onmicrosoft.com"
$MySitePrefix = "https://$($TenantName)-my.sharepoint.com"
$LogFile = '.\$TenantName\$TenantName-MySites.txt'
$MySiteListFile = '.\$TenantName\$TenantName-MySites.txt'
Connect-SPOService -Url $AdminURI -Credential $cred
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
$sstr = ConvertTo-SecureString -string $AdminPass -AsPlainText –Force
$AdminPass = ""
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminAccount, $sstr)
$proxyaddr = "$AdminURI/_vti_bin/UserProfileService.asmx?wsdl"
$UserProfileService= New-WebServiceProxy -Uri $proxyaddr -UseDefaultCredential False
$UserProfileService.Credentials = $creds
$strAuthCookie = $creds.GetAuthenticationCookie($AdminURI)
$uri = New-Object System.Uri($AdminURI)
$container = New-Object System.Net.CookieContainer
$container.SetCookies($uri, $strAuthCookie)
$UserProfileService.CookieContainer = $container
$UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)
Write-Host "Starting- This could take a while."
Out-File $LogFile -Force
$NumProfiles = $UserProfileService.GetUserProfileCount()
$i = 1
While ($UserProfileResult.NextValue -ne -1) 
{
Write-Host "Examining profile $i of $NumProfiles"
$Prop = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "PersonalSpace" } 
$Url= $Prop.Values[0].Value
if ($Url) {
$Url | Out-File $LogFile -Append -Force
}
$UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)
$i++
}
Write-Host "Done!"
$reader = [System.IO.File]::OpenText($MySiteListFile)
try {
    for(;;) {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
        $fullsitepath = "$MySitePrefix$line"
Write-Host "Operating on $fullsitepath "
$fullsitepath = $fullsitepath.trimend("/")
Write-Host "Making $eDiscoveryUser a Site Collection Admin"
Set-SPOUser -Site $fullsitepath -LoginName $eDiscoveryUser -IsSiteCollectionAdmin $true
    }
}
finally {
    $reader.Close()
}
Disconnect-SPOService
Write-Host "Done!"

0 个答案:

没有答案
相关问题