脚本工作正常,直到我尝试循环它

问题描述 投票:0回答:1

我正在尝试从 SharePoint 导出多个列表。

我首先测试了此代码的一个版本,没有循环,它按预期工作,提供文档列表和所有可用的元数据。但是当我尝试循环它时,我得到了一个正确命名的文件,但它是空白的。我没有收到任何错误。

Function MainProcress()
{
#$siteUrl = "https://XXX.sharepoint.com/sites/XXX"
# $listUrl = "Shared%20Documents"
# $outputFilePath = "C:\ACG\Expot to Excel Powershell\Listitems.csv"
    

    $includeHiddenField = $False
    $includeReadonlyField = $False

    #Install-Module -Name PnP.PowerShell

    Connect-PnPOnline -Url $siteUrl -UseWebLogin

    #Get-PnPList

    $ctx = Get-PnPContext
    $ctx.Load($ctx.Web)
    $ctx.ExecuteQuery()

    #$listFields = Get-PnPField -List $listUrl |? { $_.Hidden -eq $includeHiddenField -AND $_.ReadOnlyField -eq $includeReadonlyField} | Select -ExpandProperty internalname
    #$listFields = Get-PnPField -List $listUrl 

    $count = 0

    $listItems= GetAllListItems -ctx $ctx -listUrl $listUrl

    $listItems.Count
    $hashTable=@()  

    # Loop through the list items  
    foreach($listItem in $listItems)  
    {   $count = $count+1
        Write-Progress -Activity "Exporting" -Status "$($count/$listItems.Count*100)% Complete:" -PercentComplete $($count/$listItems.Count*100)
        $obj=New-Object PSObject              

        $listItem.FieldValues.GetEnumerator() | Where-Object { $_.Key -in $listFields }| ForEach-Object{ 
                                        
                    if($_.Value.LookupValue){$obj | Add-Member Noteproperty $_.Key $_.Value.LookupValue}
                    else{$obj | Add-Member Noteproperty $_.Key $_.Value}                                        
                    
                   
                }
        
        $hashTable+=$obj;  
        $obj=$null;  
    }  
     $hashtable | export-csv $outputFilePath -NoTypeInformation  

}

$sitelist = import-csv "C:\ACG\Expot to Excel Powershell\AllLists.csv"


ForEach ($site in $sitelist)
{
$siteUrl = $site.site
 $listUrl = $site.list
 $outputFilePath = "C:\ACG\Expot to Excel Powershell\"+$site.name+"-allitems.csv"

 MainProcress
}
powershell loops export-to-csv
1个回答
0
投票

好吧,我不小心注释掉了我需要的一行:

$listFields = Get-PnPField -List $listUrl |? { $_.Hidden -eq $includeHiddenField -AND $_.ReadOnlyField -eq $includeReadonlyField} | Select -ExpandProperty internalname
© www.soinside.com 2019 - 2024. All rights reserved.