2016-08-20 149 views
0

我一直在努力的進口CSV從的TechNet加入到一個腳本來創建的SharePoint Online子網站。最終結果是由CSV創建子網站。例如。 7年級,8年9CSV值覆蓋

我也做了以下內容:

Import-Csv 'C:\sp\import.csv'|` 
    ForEach-Object{ 
    $Urlsub = $_.Urlsub 
    $Title = $_.Title 
    } 
$wci.Url = $Urlsub 
$wci.Title = $Title 

的CSV:

Urlsub Title 
------ ----- 
Year7 Year 7 
Year8 Year 8 

這工作正常只在CSV中的最後一行。它似乎覆蓋了我最後一行的所有內容。

如果我將它更改爲: $ Urlsub + = $ _。Urlsub 它會將$ Urlsub的所有列添加到同一個數組中。

我怎麼能去導入CSV沒有它覆蓋先前的價值?

不CSV完整的腳本:

#Credentials to connect to office 365 site collection url 
$url ="xxx" 
$username="xxx" 
$password="xxx" 
$Password = $password |ConvertTo-SecureString -AsPlainText -force 

Write-Host "Load CSOM libraries" -foregroundcolor black -backgroundcolor yellow 
Set-Location $PSScriptRoot 
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll") 
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll") 


Write-Host " CSOM libraries" -foregroundcolor black -backgroundcolor yellow 
Set-Location $PSScriptRoot 
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll") 
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll") 
Write-Host "CSOM libraries loaded successfully" -foregroundcolor black -backgroundcolor Green 

Write-Host "authenticate to SharePoint Online Tenant site $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow 
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
$Context.Credentials = $credentials 
$context.RequestTimeOut = 5000 * 60 * 10; 
$web = $context.Web 
$site = $context.Site 
$context.Load($web) 
$context.Load($site) 
try 
{ 
    $context.ExecuteQuery() 
    Write-Host "authenticateed to SharePoint Online site collection $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green 
} 
catch 
{ 
    Write-Host "Not able to authenticateed to SharePoint Online site collection $url $_.Exception.Message" -foregroundcolor black -backgroundcolor Red 
    return 
} 


#creating site using WebCreationInformation calss 
Write-Host "creating subsite using custom webtemplate" -foregroundcolor black -backgroundcolor yellow 
$wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation 
$wci.Url = "Year7" 
$wci.Title = "Year 7" 
$wci.UseSamePermissionsAsParentSite = $true 
$wci.WebTemplate = "{D0714A63-356A-4B73-815B-6E1DF824237F}#Template" 
$wci.Language = 1033 
$blogWeb = $site.RootWeb.Webs.Add($wci); 
try 
{ 
$context.ExecuteQuery(); 
Write-Host "Sub site created successfully using custom webtemplate" -foregroundcolor black -backgroundcolor Green 
} 
catch 
{ 
Write-Host "Error while creating the Sub site using custom webtemplate" $_.Exception.Message -foregroundcolor black -backgroundcolor RED 
} 

回答

0

你結束了最後的價值,因爲你的變量賦值落在它讀取CSV文件中的循環之外。因此,您將獲取變量設置的最後一項內容(CSV中的最後一行)。

我希望您需要爲csv文件中的每一行創建WebCreationInformation的實例,然後將這些實例中的每一個添加到$site.RootWeb.Webs中,正如我在本示例中所做的那樣。

Import-Csv 'C:\sp\import.csv' | ForEach-Object{ 
    $wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation 
    $wci.Url = $_.Urlsub 
    $wci.Title = $_.Title 
    $wci.UseSamePermissionsAsParentSite = $true 
    $wci.WebTemplate = "{D0714A63-356A-4B73-815B-6E1DF824237F}#Template" 
    $wci.Language = 1033 
    $blogWeb = $site.RootWeb.Webs.Add($wci); 
}