Powershell-将sharepoint文档子文件夹中的文件移动到网络共享 [英] Powershell- move files in sharepoint documents subfolder to network share

查看:106
本文介绍了Powershell-将sharepoint文档子文件夹中的文件移动到网络共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是PowerShell的新手,也是Sharepoint的新手。我的目标是将所有文件移动到名为"签名票证"的Sharepoint文档子文件夹中。现在,我有一个成功复制文档库中所有文件的脚本。
我还想在源目标中创建一个新文件夹,目前正在脚本中完成。我只需要这些文件。

I am pretty new to powershell and a complete novice with Sharepoint. My goal is to move all files in a Sharepoint documents subfolder called "Signed tickets". Right now, I have a script that is successfully copying ALL files in the document library. I also would like to do away with the creation of a new folder in the source destination which is currently being done in the script. I just need the files.


我尝试将copyto部分更改为moveto但是说失败说[System.Net.ConnectStream]不包含名为'的方法搬去'。我已经复制了下面的脚本。非常感谢任何帮助!!

I tried changing the copyto part to moveto but that failed saying [System.Net.ConnectStream] does not contain a method named 'MoveTo'. I've copied the script below. Any help is greatly appreciated!!


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Download-AllFilesFromLibrary()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
        [Parameter(Mandatory=$true)] [string] $TargetFolder
    )
    Try {
         
        #Create Local Folder, if it doesn't exist
        $FolderName = ($SourceFolder.ServerRelativeURL) -replace "/","\"
        $LocalFolder = $TargetFolder + $FolderName
        If (!(Test-Path -Path $LocalFolder)) {
                New-Item -ItemType Directory -Path $LocalFolder | Out-Null
        }
         
        #Get all Files from the folder
        $FilesColl = $SourceFolder.Files
        $Ctx.Load($FilesColl)
        $Ctx.ExecuteQuery()

        #Iterate through each file and download
        Foreach($File in $FilesColl)
        {
            $TargetFile = $LocalFolder+"\"+$File.Name
            #Download the file
            $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$File.ServerRelativeURL)
            $WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)
            $FileInfo.Stream.CopyTo($WriteStream)
            $WriteStream.Close()
            write-host -f Green "Downloaded File:"$TargetFile
        }
         
        #Process Sub Folders
        $SubFolders = $SourceFolder.Folders
        $Ctx.Load($SubFolders)
        $Ctx.ExecuteQuery()
        Foreach($Folder in $SubFolders)
        {
            If($Folder.Name -ne "Forms")
            {
                #Call the function recursively
                Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $Folder -TargetFolder $TargetFolder
            }
        }
  }
    Catch {
        write-host -f Red "Error Downloading Files from Library!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://advhomecare.sharepoint.com/teams/CPRPlusPST/"
$LibraryName="Documents"
$TargetFolder="\\cpr-appsprod\cprsql\incoming"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
      
#Get the Library
$List = $Ctx.Web.Lists.GetByTitle($LibraryName)
$Ctx.Load($List)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()

 
#Call the function to download file 
Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $List.RootFolder -TargetFolder $TargetFolder 






推荐答案

Stream对象没有MoveTo方法。

The Stream object don’t have MoveTo method.

https: //docs.microsoft.com/en-us/dotnet/api/system.io.stream?redirectedfrom=MSDN&view=netframework-4.7.2

虽然我测试了我本地的脚本,如果用户帐户有权共享文件夹,脚本可以从SharePoint下载文件并保存到共享文件夹。

While I tested the script in my local, if user account has permission to share folder, the script could download files form SharePoint and save to share folder.




#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Download-AllFilesFromLibrary()
{ 
    param
    (
        [Parameter(Mandatory=


true )] [string]
true)] [string]


SiteURL,
[参数(强制性=
SiteURL, [Parameter(Mandatory=


这篇关于Powershell-将sharepoint文档子文件夹中的文件移动到网络共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆