【问题标题】:CSV value being overwrittenCSV 值被覆盖
【发布时间】:2016-12-27 13:19:51
【问题描述】:

我一直在尝试将 import-csv 添加到来自 technet 的脚本中,以在 SharePoint Online 上创建子网站。最终结果将是由 CSV 制作子网站。例如。七年级,八年级。九年级。

我做了以下事情:

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
}

【问题讨论】:

    标签: powershell sharepoint office365 sharepoint-online


    【解决方案1】:

    您最终得到最后一个值,因为您的变量赋值不在读取 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 2013-09-02
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多