PnP PowerShell代码语法错误的问题

时间:2019-07-12 12:31:12

标签: powershell

我有一个遍历文档库并创建一个列出与每个项目相关的元数据的报告的功能。

它不会编译。在第一括号的函数声明中,出现错误:

Missing closing '}' in statement block or type definition

我已经识别出似乎是错误源的代码行。当我在#之间的行上剪切代码时,错误消失了。

我找不到语法不正确的地方。我正在使用:

  

SharePointPnPPowerShell2013 3.11.1907.0

function PopulateData($web, $incldeFileSize) {
        Write-Host "Current Site " $web.url -ForegroundColor Cyan 
                  $libs = Get-PnPList -Web $web | Where{($_.BaseType -eq “DocumentLibrary”) -or ($_.Title -eq "Pages") }

                  foreach($lib in $libs){
                     $libitems = (Get-PnPListItem -Web $web -List $lib -Fields "FileLeafRef","Name","Title","Author","Modified","Created","KBAbstract","KBContentAuthor","KBCategory","Publish","KBPublishDate").FieldValues

                       foreach($libitem in $libitems)
                         {
                             if($libitem.FSObjType -eq "0"){
                              $data = @{
                                          "Web Name" = $web.Title
                                          "Library Name" = $lib.Title
                                          "File Name" = $libitem.FileLeafRef
                                          "Abstract" = $libitem.KBAbstract
                                          "Content Author" = $libitem.KBContentAuthor.Email

                                          ############The Problem is somewhere in these Lines of code###########
                                          [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
                                            $MMSFieldTerms = ""
                                            Foreach($MMSFieldValue in $MMSFieldValueColl)
                                            {
                                                if($MMSFieldValue.label -ne $null)
                                                {
                                                    $MMSFieldTerms+=$MMSFieldValue.label+"; "
                                                }
                                            }
                                            write-host $MMSFieldTerms
                                            ##########The Problem ends here############

                                          "Knowledge Area" = $libitem.KBCategory
                                          "Publish" = $libitem.Publish
                                          "Published Date" = $libitem.KBPublishedDate.LookupValue
                                          "File URL" = $libitem.FileRef
                                          "Modified Date" = $libitem.Modified
                                          "Created By" = $libitem.Author.LookupValue
                                          "Created Date" = $libitem.Created
                                          "Modified By" = $libitem.Editor.LookupValue
                                          "File Size (KB)" = $null
                                     }
                             if($incldeFileSize -eq $true){
                                    $file = Get-PnPFile -Url $libitem.FileRef
                                    $data["File Size (KB)"] = $file.Length / 1KB
                              }
                              New-Object PSObject -Property $data
                            }
                         }
                  }       
}

2 个答案:

答案 0 :(得分:1)

您应该移动整个方块

############The Problem is somewhere in these Lines of code###########
[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
$MMSFieldTerms = ""
Foreach($MMSFieldValue in $MMSFieldValueColl) {
    if($MMSFieldValue.label -ne $null) {
        $MMSFieldTerms+=$MMSFieldValue.label+"; "
    }
}
write-host $MMSFieldTerms
##########The Problem ends here############
$data的哈希表定义之外的

。在那里,只需要名称/值对。 如果较高,我会移动到if($libitem.FSObjType -eq "0"){下方和$data = @{之前,因此如果需要,可以将其添加到哈希表中

"Field Terms" = $MMSFieldTerms -join ';'


侧注1 :为什么不使用字符串连接,为什么不构建$MMSFieldTerms作为数组并随后将元素与';'连接起来,如下所示:

[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
$MMSFieldTerms = foreach($MMSFieldValue in $MMSFieldValueColl) {
    if($MMSFieldValue.label) {
        $MMSFieldValue.label
    }
}

write-host ($MMSFieldTerms -join ';')

侧注2 :最好将所有卷曲的“智能引号”替换为直的引号:“DocumentLibrary”-> "DocumentLibrary"

答案 1 :(得分:0)

我没有SharePoint来执行代码和测试。我唯一能看到的是您的Foreach未设置为属性。由于您使用$data来尝试构建哈希表,因此您需要将要加入的集合设置为键/值对。

所以这个:

                    ############The Problem is somewhere in these Lines of code###########
                    [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
                    $MMSFieldTerms = ""
                    Foreach($MMSFieldValue in $MMSFieldValueColl) {
                        if($MMSFieldValue.label -ne $null) {
                            $MMSFieldTerms+=$MMSFieldValue.label+"; "
                        }
                    }
                    write-host $MMSFieldTerms
                    ##########The Problem ends here############

应该是这样的:(在其周围添加了****以仅显示差异)

            ############The Problem is somewhere in these Lines of code###########
            [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
            $MMSFieldTerms = ""
            "****Some Property****" = Foreach($MMSFieldValue in $MMSFieldValueColl) {
                if($MMSFieldValue.label -ne $null) {
                    $MMSFieldTerms+=$MMSFieldValue.label+"; "
                }
            }
            write-host $MMSFieldTerms
            ##########The Problem ends here############