gpt4 book ai didi

powershell - PowerShell从CSV打印站点母版页

转载 作者:行者123 更新时间:2023-12-03 00:10:33 25 4
gpt4 key购买 nike

这是用于SharePoint Online的

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

SiteName和SiteURL

我正在尝试创建将通过CSV文件读取的Powershell脚本,并读取SiteURL。然后,让它返回网站的名称和所使用的母版页。结果可以在屏幕上返回,也可以导出到另一个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
}

运行脚本时收到此错误消息
Error When I run the script

最佳答案

假设您有$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文件和处理。一些假设如下:
  • 您有两列称为SiteNameSiteUrl
  • 您的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-凭据$ 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。

    关于powershell - PowerShell从CSV打印站点母版页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867621/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com