Powershell循环通过SharePoint库提取特定文档

时间:2017-11-23 22:39:30

标签: powershell sharepoint

我修改了在线发现的脚本,以循环访问SharePoint库并将文档作为列表提取。一切正常,我得到一个html输出文件,按用户排序和分组,并通过电子邮件发送给我。

现在我需要进一步修改此脚本,以便能够仅使用用户文档向每个用户发送电子邮件。 到目前为止,我已经为每个用户创建了单独的文件,并仅向用户发送电子邮件,只有用户文档,但每个用户报告中始终缺少第一个文档。这意味着,当用户只有一个文档时,创建的文件没有内容。

见下面的代码:

foreach($list in $alllistcol){

    if (($excludeLists -notcontains $list.Title) -and ($list.Basetype -eq "1"))
    {
        $files = GetListItems -site $site.Url -listname $list.Title

        # Loop through each file in the document library and perform the following
        #$files | Sort-Object -Property CreatedBy

        $lastAuthor = $file.ows_Author

        foreach($file in $files)
        {
            #Write-host "$($site.Title),$($list.Title),$($file.ows_LinkFileName)"
            #Write-Host "$($file.ows_LinkFileName)"

            # Use the following to add additional metadata columns

             if($lastAuthor -ne $file.ows_Author)
            {
                $AuthorFile = FileOutput
                SendEmail -outfile $AuthorFile -Author ($file.ows_Author -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
                $output = @()
                $lastAuthor = $file.ows_Author
            }else{   
                $record = new-object System.Object
                #$record | Add-Member -type NoteProperty -name Site -value $site.Title
                #$record | Add-Member -type NoteProperty -name List -value $list.Title
                #$record | Add-Member -type NoteProperty -name FileTitle -value $file.ows_Title
                $record | Add-Member -type NoteProperty -name Filename -value $file.ows_LinkFileName
                #$record | Add-Member -type NoteProperty -name Created -value $file.ows_Created
                $record | Add-Member -type NoteProperty -name CreatedBy -value ($file.ows_Author -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
                $record | Add-Member -type NoteProperty -name ExpiryDate -value $file.ows_Expiry_x0020_Date
                #$record | Add-Member -type NoteProperty -name Modified -value $file.ows_Modified
                #$record | Add-Member -type NoteProperty -name ModifiedBy -value $file.ows_Editor
                #$record | Add-Member -type NoteProperty -name ContentType -value $file.ows_ContentType
                #if ($file.ows_File_x0020_Size)
                #{
                #    $record | Add-Member -type NoteProperty -name Filesize -value $file.ows_File_x0020_Size.split("([^#]+$)'")[-1]
                #}
                $output += $record
                $countfiles++

                #$output += "$($site.Title),$($list.Title),$($file.ows_Title),$($file.ows_LinkFileName),$($file.ows_Created)"
            }
        }
    }
} 

我认为问题在于:$ lastAuthor = $ file.ows_Author在实际声明$ file之前。如何比较文档的作者关闭并打开一个新文件而不跳过每个作者的第一个文档?

----------------------------------------------- ----------------更新--------------------------------- ---------------------------

我已经修复了现在跳过第一个文档的问题。

if($lastAuthor -ne $file.ows_Author)
            {
                FileOutput
                SendEmail -Author ($file.ows_Author -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
                #$output = $null
                $output = @()
                #$lastAuthor = $null
                $lastAuthor = $file.ows_Author
                $record = new-object System.Object
                $record | Add-Member -type NoteProperty -name Filename -value $file.ows_LinkFileName
                $record | Add-Member -type NoteProperty -name CreatedBy -value ($file.ows_Author -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
                $record | Add-Member -type NoteProperty -name ExpiryDate -value $file.ows_Expiry_x0020_Date
                $output += $record
                $countfiles++
            }else{   
                $record = new-object System.Object
                #$record | Add-Member -type NoteProperty -name Site -value $site.Title
                #$record | Add-Member -type NoteProperty -name List -value $list.Title
                #$record | Add-Member -type NoteProperty -name FileTitle -value $file.ows_Title
                $record | Add-Member -type NoteProperty -name Filename -value $file.ows_LinkFileName
                #$record | Add-Member -type NoteProperty -name Created -value $file.ows_Created
                $record | Add-Member -type NoteProperty -name CreatedBy -value ($file.ows_Author -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
                $record | Add-Member -type NoteProperty -name ExpiryDate -value $file.ows_Expiry_x0020_Date
                #$record | Add-Member -type NoteProperty -name Modified -value $file.ows_Modified
                #$record | Add-Member -type NoteProperty -name ModifiedBy -value $file.ows_Editor
                #$record | Add-Member -type NoteProperty -name ContentType -value $file.ows_ContentType
                #if ($file.ows_File_x0020_Size)
                #{
                #    $record | Add-Member -type NoteProperty -name Filesize -value $file.ows_File_x0020_Size.split("([^#]+$)'")[-1]
                #}
                $output += $record
                $countfiles++

                #$output += "$($site.Title),$($list.Title),$($file.ows_Title),$($file.ows_LinkFileName),$($file.ows_Created)"
            }

不幸的是我发现了另一个问题:我的查询按作者排序但不知道它不会循环通过最后一个作者。例如:我有4位作者 - Amber,Mary,Sally和Wayne。 Amber,Mary和Sally一切都很好。他们的文件正确创建,他们的电子邮件正确发送出去。现在Wayne的文件没有内容,他的原因电子邮件也没有文件显示。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我自己解决了问题:D

if (($excludeLists -notcontains $list.Title) -and ($list.Basetype -eq "1"))
    {
        $files = GetListItems -site $site.Url -listname $list.Title

        # Loop through each file in the document library and perform the following
        #$files | Sort-Object -Property CreatedBy

        $lastAuthor = $null

        foreach($file in $files)
        {
            #Write-host "$($site.Title),$($list.Title),$($file.ows_LinkFileName)"
            #Write-Host "$($file.ows_LinkFileName)"

            # Use the following to add additional metadata columns

            if($lastAuthor -eq $null)
            {
                $lastAuthor = $file.ows_Author
            }

            if($lastAuthor -ne $file.ows_Author)
            {
                FileOutput
                SendEmail -Author ($lastAuthor -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
                #$output = $null
                $output = @()
                #$lastAuthor = $null
                $lastAuthor = $file.ows_Author
            }
            $record = new-object System.Object
            #$record | Add-Member -type NoteProperty -name Site -value $site.Title
            #$record | Add-Member -type NoteProperty -name List -value $list.Title
            #$record | Add-Member -type NoteProperty -name FileTitle -value $file.ows_Title
            $record | Add-Member -type NoteProperty -name Filename -value $file.ows_LinkFileName
            #$record | Add-Member -type NoteProperty -name Created -value $file.ows_Created
            $record | Add-Member -type NoteProperty -name CreatedBy -value ($file.ows_Author -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
            $record | Add-Member -type NoteProperty -name ExpiryDate -value $file.ows_Expiry_x0020_Date
            #$record | Add-Member -type NoteProperty -name Modified -value $file.ows_Modified
            #$record | Add-Member -type NoteProperty -name ModifiedBy -value $file.ows_Editor
            #$record | Add-Member -type NoteProperty -name ContentType -value $file.ows_ContentType
            #if ($file.ows_File_x0020_Size)
            #{
            #    $record | Add-Member -type NoteProperty -name Filesize -value $file.ows_File_x0020_Size.split("([^#]+$)'")[-1]
            #}
            $output += $record
            $countfiles++

            #$output += "$($site.Title),$($list.Title),$($file.ows_Title),$($file.ows_LinkFileName),$($file.ows_Created)"

            if($files.IndexOf($file) -eq ($files.Count -1))
            {
                FileOutput
                SendEmail -Author ($lastAuthor -replace '[ ](?=[ ])|[^-_,A-Za-z ]+', '')
            }
        }
    }

最后一个是检查文件是否是foreach迭代中的最后一个并创建文件并向最后一个作者发送电子邮件。