【问题标题】:Small Issues with powershell Script that loops over folders in Sharepoint循环遍历 Sharepoint 中文件夹的 powershell 脚本的小问题
【发布时间】:2021-12-21 02:21:40
【问题描述】:

此脚本循环遍历 powershell 中的每个文件夹并检查该文件夹是否具有通配符文本,然后将文件位置输出到 csv。

我得到的错误是:Export-Csv:无法将参数绑定到参数“InputObject”,因为它为空。

还有一些包含通配符的文件夹。大多数情况下,在级别 3 - 4 - 5 中,它甚至在输出中都没有检测到强硬,我看到一个包含该单词的文件夹。 提前致谢!

Import-Module PnP.PowerShell

#Config Parameters
$SiteURL= "*******/sites/Archief"
$Folder = "Gedeelde Documenten"
$Pagesize = "500"
Connect-PnPOnline -Url $SiteURL -Interactive ### Change to your specific site ###

#Output path
$outputPath = "C:\users\$env:USERNAME\Desktop\test.csv"



Function GetFolders($folderUrl)  
{      
    $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder   
    # Loop through the folders  
    foreach($folder in $folderColl)  
    {                      
       $newFolderURL= $folderUrl+"/"+$folder.Name
       if ($newFolderURL -cnotlike "*archief*"){
           write-host -ForegroundColor Green $folder.Name " - " $newFolderURL
           $DocLibs = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101} 
            #Loop thru each document library & folders
            $results = @()
            foreach ($DocLib in $DocLibs) {
                if ($DocLib -cnotlike "*archief*"){
                    $AllItems = Get-PnPListItem -List $DocLib -Fields "FileRef", "File_x0020_Type", "FileLeafRef" -PageSize 500
                    #Loop through each item
                    foreach ($Item in $AllItems) {
                        if ($Item["FileRef"] -cnotlike "*archief*" -or $Item["FileLeafRef"] -cnotlike "*archief*" -and ($Item["File_x0020_Type"])){
                            Write-Host "File found. Path:" $Item["FileRef"] -ForegroundColor Green               
                            #Creating new object to export in .csv file
                            $results += New-Object PSObject -Property @{
                                Path          = $Item["FileRef"]
                                FileName      = $Item["FileLeafRef"]
                                FileExtension = $Item["File_x0020_Type"]
                           }
                    }
                 }
            }
    }
           GetFolders($newFolderURL)
        }
    }           
}

$results | Export-Csv -Path $outputPath -NoTypeInformation

GetFolders($folder)  

【问题讨论】:

    标签: powershell sharepoint


    【解决方案1】:

    您的代码有一些问题。首先,您首先将结果输出到 CSV,然后调用该函数。您还可以遍历文件夹,并不断创建一个永远不会输出的新结果对象。最后,您从函数内部调用函数本身。您在 GetFolders 函数内的 foreach 循环中调用 GetFolders。

    有几种方法可以解决这个问题,例如让 CSV 从函数内部填充并附加新数据。

    Import-Module PnP.PowerShell
    
    #Config Parameters
    $SiteURL= "*******/sites/Archief"
    $Folder = "Gedeelde Documenten"
    $Pagesize = "500"
    Connect-PnPOnline -Url $SiteURL -Interactive ### Change to your specific site ###
    
    #Output path
    $outputPath = "C:\users\$env:USERNAME\Desktop\test.csv"
    
    
    Function GetFolders{      
        [CmdletBinding()]
        param(
            [Parameter(Mandatory=$true)]
            $FolderUrl,
            
            [Parameter(Mandatory=$true)]
            $OutputPath
        )
        
        $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder   
        # Loop through the folders  
        foreach($folder in $folderColl){                      
            $newFolderURL= $folderUrl+"/"+$folder.Name
            if ($newFolderURL -cnotlike "*archief*"){
                write-host -ForegroundColor Green $folder.Name " - " $newFolderURL
                $DocLibs = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101} 
                #Loop thru each document library & folders
                $results = @()
                foreach($DocLib in $DocLibs){
                    if($DocLib -cnotlike "*archief*"){
                        $AllItems = Get-PnPListItem -List $DocLib -Fields "FileRef", "File_x0020_Type", "FileLeafRef" -PageSize 500
                        #Loop through each item
                        foreach ($Item in $AllItems) {
                            if ($Item["FileRef"] -cnotlike "*archief*" -or $Item["FileLeafRef"] -cnotlike "*archief*" -and ($Item["File_x0020_Type"])){
                                Write-Host "File found. Path:" $Item["FileRef"] -ForegroundColor Green               
                                #Creating new object to export in .csv file
                                $results += New-Object PSObject -Property @{
                                    Path          = $Item["FileRef"]
                                    FileName      = $Item["FileLeafRef"]
                                    FileExtension = $Item["File_x0020_Type"]
                                }
                            }
                        }
                    }
                }
                $results | Export-Csv -Path $outputPath -NoTypeInformation -Append
                GetFolders($newFolderURL)
            }
        }            
    }
    
    GetFolders -FolderUrl $Folder -OutputPath $outputPath
    

    如您所见,输出路径已添加到函数中,并且在每次循环后将结果推送到 CSV。

    您也可以让结果在函数内创建,然后返回到初始调用:

    Import-Module PnP.PowerShell
    
    #Config Parameters
    $SiteURL= "*******/sites/Archief"
    $Folder = "Gedeelde Documenten"
    $Pagesize = "500"
    Connect-PnPOnline -Url $SiteURL -Interactive ### Change to your specific site ###
    
    #Output path
    $outputPath = "C:\users\$env:USERNAME\Desktop\test.csv"
    
    
    Function GetFolders{      
        [CmdletBinding()]
        param(
            [Parameter(Mandatory=$true)]
            $FolderUrl
        )
        
        $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder   
        # Loop through the folders 
        $results = @()
        foreach($folder in $folderColl){                      
            $newFolderURL= $folderUrl+"/"+$folder.Name
            if ($newFolderURL -cnotlike "*archief*"){
                write-host -ForegroundColor Green $folder.Name " - " $newFolderURL
                $DocLibs = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101} 
                #Loop thru each document library & folders
                foreach($DocLib in $DocLibs){
                    if($DocLib -cnotlike "*archief*"){
                        $AllItems = Get-PnPListItem -List $DocLib -Fields "FileRef", "File_x0020_Type", "FileLeafRef" -PageSize 500
                        #Loop through each item
                        foreach ($Item in $AllItems) {
                            if ($Item["FileRef"] -cnotlike "*archief*" -or $Item["FileLeafRef"] -cnotlike "*archief*" -and ($Item["File_x0020_Type"])){
                                Write-Host "File found. Path:" $Item["FileRef"] -ForegroundColor Green               
                                #Creating new object to export in .csv file
                                $results += New-Object PSObject -Property @{
                                    Path          = $Item["FileRef"]
                                    FileName      = $Item["FileLeafRef"]
                                    FileExtension = $Item["File_x0020_Type"]
                                }
                            }
                        }
                    }
                }
                GetFolders($newFolderURL)
            }
        }
        return $results            
    }
    
    GetFolders -FolderUrl $Folder | Export-Csv -Path $outputPath -NoTypeInformation 
    

    我还建议在 Export-CSV 中使用 '-Delimiter ";"',这样当您在 Excel 中打开文件时,它会自动排序。

    【讨论】:

    • 谢谢!这行得通。遍历文件夹时,我怎么会遇到通配符问题?
    • 您遇到的错误是什么?
    • 其实没有。但例如:它遍历共享点中的每个文件夹。如果 1 个文件夹包含大写或小写的“archief”一词。它可能不会进入该文件夹或不列出它。最终目的是我们删除不在名为“archief”的文件夹中的所有内容
    • 您应该仔细查看您的脚本,因为说实话仍然没有多大意义。您遍历文件夹,但实际上不做任何事情,只是用它输出名称。然后 foreach 文件夹,您将获得包含所有 docLibs 的列表并一遍又一遍地查询相同的项目...只需在函数之外从小处着手并逐步创建逻辑将是我的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多