【问题标题】:PowerShell to Print Sites MasterPage from a CSVPowerShell 从 CSV 打印站点 MasterPage
【发布时间】:2018-09-26 19:14:35
【问题描述】:

适用于 SharePoint Online

我有一个列出网站名称及其 URL 的 CSV。它包含 2 列:

站点名称和站点 URL

我正在尝试创建一个 powershell 脚本,该脚本将读取 CSV 文件,读取 SiteURL。然后让它返回站点的名称和它正在使用的 MasterPage。结果可以在屏幕上返回或导出到另一个 CSV。

以下是我正在尝试的代码,但我无法添加正确的 CMDLETS。我是 PowerShell 的新手,任何帮助将不胜感激。

connect-SPOService -Url $adminUrl -Credential $pscreds

$fileToRead= Import-CSV -Path C:\...\sitemasterpages.csv

foreach($site in $fileToRead)
{
    $web = Get-Pnpweb($site.SiteUrl); 
    $masterPage = $web.GetFile($web.MasterUrl);
    $masterPage.Name 
    $web.Title 
}

我在运行脚本时收到此错误消息

【问题讨论】:

    标签: powershell csv parsing sharepoint master-pages


    【解决方案1】:

    假设您有$siteurls 的集合,您可以尝试类似以下的操作:

    foreach($url in $siteurls)
    {
        $web = Get-SPWeb($url);
        $masterPage = $web.GetFile($web.MasterUrl);
        $masterPage.Name //name of the masterpage
        $web.Title //name of the website
    }
    

    让我知道这是否有效。

    编辑

    更新答案:这包括读取 CSV 文件并逐行处理。一些假设如下:

    1. 您有两个名为SiteNameSiteUrl 的列
    2. 您的 CSV 文件实际上是用逗号分隔的。如果不是,您需要在读取 CSV 文件时使用-delimeter 开关。

    更新后的代码现在是:

    $fileToRead= Import-CSV -Path <absolute path of your CSV file here>
    
    foreach($site in $fileToRead)
    {
        $web = Get-SPWeb($site.SiteUrl); //this reads the SiteUrl value in the current line
        $masterPage = $web.GetFile($web.MasterUrl);
        $masterPage.Name //name of the masterpage
        $web.Title //name of the website
    }
    

    编辑

    更新了使用 SharePoint PNP 的答案

    connect-SPOService -Url $adminUrl -Credential $pscreds

    $fileToRead= Import-CSV -Path C:\...\sitemasterpages.csv
    
    foreach($site in $fileToRead)
    {
        $web = Get-Pnpweb($site.SiteUrl); 
        $masterPage = $web.Get-Pnpfile($web.MasterUrl);
        $masterPage.Name 
        $web.Title 
    }
    

    请注意,我使用了基于 SharePoint PnP here 文档的 Get-Pnpfile。

    【讨论】:

    • 是的,我有一个包含 2 列的 CSV。站点名称和站点 URL。我正在尝试让 Powershell 查看 CSV 内部 > 阅读“SiteURL”列 > 然后打印到屏幕或将其使用的 URL 和母版页导出到另一个 CSV。
    • 我已经更新了我的答案,包括阅读 CSV 文件。让我知道这是否适合您。
    • 请原谅,我对 powershell 还是很陌生。我不断收到有关 Getfile 方法调用失败的错误消息。 “方法调用失败,因为 [Microsoft.SharePoint.Client.Web] 不包含名为 'GetFile' 的方法。”
    • 你能用你的整个代码更新你的问题,并准确指出错误的来源吗?
    • 我根据您的代码更新了我的答案。抱歉,我一开始没有注意到您使用的是 PnP。我刚刚用 Get-Pnpfile 替换了 GetFile,它现在应该可以工作了。如果您仍有问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多