2017-11-15 8 views
0

SharePointテナントでサイトのコレクションを生成するPowerShellスクリプトを実行しようとしています。ただし、サイトコレクションの作成を実行しようとすると、エラーが発生します。CSOM PowerShellを使用してサイトコレクションを作成しようとするとエラーが発生する

Exception calling "ExecuteQuery" with "0" argument(s): "Unknown Error" At C:\projects\SP\Scripts\Taxonomy\Add-NavigationTerms1.ps1:120 char:5 + $TenantContext.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException

どうすればこのエラーを回避できますか?どうすれば修正できますか?すべてのあなたの助けをありがとう!ここで

はコードです:

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() 
+0

うまく働いていましたSOコードのハイライターは '\ 'がエスケープ文字であると考えるので、読みやすく、ハイライトしやすくなります。したがって、PowerShellは両方をサポートしているので、 '' \ ''を '/'に置き換えました。 – Clijsters

+0

コメントしたコードの一部を削除してください。 [最小限で完全で検証可能なサンプルの作成方法](https://stackoverflow.com/help/mcve)を見てください。 – Clijsters

答えて

0

私は正直に何が悪かったのかわかりませんが、このコード/方法は、私はにそれを容易にするために、あなたのコードを少し再フォーマット

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 
    } 
} 
関連する問題