首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用CSOM PowerShell创建站点集合时出错

尝试使用CSOM PowerShell创建站点集合时出错
EN

Stack Overflow用户
提问于 2017-11-15 06:18:22
回答 1查看 627关注 0票数 0

我正在尝试运行一个PowerShell脚本,该脚本将在SharePoint租户上生成站点收集信息。但是,每当我试图执行站点集合创建时,我都会得到一个错误:

在C:\projects\SP\Scripts\Taxonomy\Add-NavigationTerms1.ps1:120 char:5 + $TenantContext.ExecuteQuery() +~+ CategoryInfo : NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId : ServerException处调用带有"0“参数:”未知错误“的异常

我将如何避免这个错误?我怎么才能修好它?非常感谢你的帮助!

以下是代码:

代码语言:javascript
复制
param (    
    [Parameter(Mandatory = $false)]    
    [string]    $sourceWeb = '',    
    [string]    $destinationWeb = '',       
    [Parameter(Mandatory = $false)]    
    [string]    $sourceListName = 'CurrentSites', #$sourceListName = 'O365 Sites',       
    [Parameter(Mandatory = $false)]    
    [string]    $sharepointAdmin = ''
)

<# 
Add-Type -Path "C:/Program Files/SharePoint Client Components/Assemblies/Microsoft.Online.SharePoint.Client.Tenant.dll" 
Add-Type -Path "C:/Program Files/Common Files/microsoft shared/Web Server Extensions/16/ISAPI/Microsoft.SharePoint.Client.dll"   
Add-Type -Path "C:/Program Files/Common Files/microsoft shared/Web Server Extensions/16/ISAPI/Microsoft.SharePoint.Client.Runtime.dll"   
Add-Type -Path "C:/Program Files/Common Files/microsoft shared/Web Server Extensions/16/ISAPI/Microsoft.SharePoint.Client.UserProfiles.dll"  
#>

$path = 'C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/15/ISAPI/' 
$simpleLinkUrlPropertyName = '_Sys_Nav_SimpleLinkUrl' 
$assemblies = 'Microsoft.SharePoint.Client.dll', 
'Microsoft.SharePoint.Client.Runtime.dll', 
'Microsoft.SharePoint.Client.Taxonomy.dll' 

$assemblies | % { Add-Type -Path (Join-Path $path $_) }

Add-Type -Path "C:/Program Files/SharePoint Client Components/Assemblies/Microsoft.Online.SharePoint.Client.Tenant.dll"

#$context = New-Object Microsoft.SharePoint.Client.ClientContext($sourceWeb)
#$password = Read-Host -Prompt 'Please enter your password' -AsSecureString
#$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sharepointAdmin,$password)

$password = ''


#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


Function Get-SPOContext([string]$Url, [string]$UserName, [string]$Password) {
        $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
        return $context 
}

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {

        $list = $Context.Web.Lists.GetByTitle($listTitle)
        $Context.Load($list)
        $Context.ExecuteQuery()

        $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $items = $list.GetItems($qry)
        $Context.Load($items)
        $Context.ExecuteQuery()
        return $items  
}

Function Create-Site-Collection([String]$url, [String]$title) {
        $TenantURL = ''
        $Title = 'New Published Collection List';

        [string]
        $TenantUserName = '';

        [Security.SecureString]
        #$TenantPassword = ConvertTo-SecureString 'Anew6731' -AsPlainText -Force
        $TenantPassword = convertto-securestring "Anew6731" -asplaintext -force

        #Open the Tenant Administration Context with the Tenant Admin Url
        $TenantContext = Get-SPOContext -Url $TenantUrl -UserName $TenantUserName -Password "Anew6731"


        #$TenantContext = New-Object Microsoft.SharePoint.Client.ClientContext($TenantUrl)
        #$TenantCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($TenantUserName, $TenantPassword)
        #$TenantContext.Credentials = $TenantCredentials

        #Get the tenant object
        $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($TenantContext)


        #Set the Site Creation Properties values
        $TenantProperties = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
        #$TenantProperties.Url = $url
        $TenantProperties.Template = "CMSPUBLISHING#0 "
        $TenantProperties.Owner = $TenantUserName
        $TenantProperties.StorageMaximumLevel = 1000
        $TenantProperties.UserCodeMaximumLevel = 300
        $TenantProperties.Title = $title

        $TenantContext.Load($Tenant)
        $TenantContext.ExecuteQuery()

        #$TenantContext.Load($TenantProperties)
        #$TenantContext.ExecuteQuery()

        #Create the site using the properties

        $Tenant.CreateSite($TenantProperties) | Out-Null

        Write-Host "Creating site collection "
        #Create the site in the tennancy
        #$TenantContext.Load($Tenant)
        #$TenantContext.Load($spOnlineOperation)
        $TenantContext.ExecuteQuery()
        #$TenantContext.Dispose()    
}


$UserName = $sharepointAdmin
#$Password = Read-Host -Prompt "Enter the password"     
$Url = $sourceWeb

$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password $items = Get-ListItems -Context $context -ListTitle "O365 Sites"  

foreach ($item in $items) {    
        $Context.Load($item)    
        $Context.ExecuteQuery()   
        Write-Host $item["NEWURL"]    
        Write-Host $item["Title"]    
        #Write-Host $item.FieldValues    
        Create-Site-Collection -URL $item["NEWURL"] -Title $item["Title"]   
        #Write-Hsost $item.DisplayName 

} 

$context.Dispose()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-17 17:31:25

老实说,我不知道哪里出了问题,但是这个代码/方法运行得很好。

代码语言:javascript
复制
Function Create-Site-Collection([String]$fullUrl, [Microsoft.SharePoint.Client.ClientContext] $TenantContext)
{
    Write-Host "Now configuring the new Site Collection"

    #Get the tenant object
    $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($TenantContext)

    #Set the Site Creation Properties values
    $properties = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
    $properties.Url = $fullUrl
    $properties.Template =  "BLANKINTERNETCONTAINER#0"
    $properties.Owner = $AdminSiteUsername
    $properties.StorageMaximumLevel = 1000
    #$properties.UserCodeMaximumLevel = 100
    $properties.TimeZoneId = 10 # (UTC-05:00) Eastern Time (US and Canada)


    #Create the site using the properties
    $tenant.CreateSite($properties) | Out-Null

    $TenantContext.ExecuteQuery()

    Write-Host "Creating site collection"
    #Create the site in the tennancy
    try
    {
    $TenantContext.ExecuteQuery()
    Write-Host "Site Creation request completed. Note that the creation process is asynchronous and provisioning may take a short while."
    }
    Catch [Exception] 
    {
        Write-host $_.Exception.Message -f Red
    } 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47300626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档