Powershell Sharepoint下载文件夹无法正常工作

时间:2018-06-28 15:06:25

标签: powershell

具有一个导入和导出文件的脚本。脚本没有出现任何错误,但是文件夹中的文件没有下载。注意,我只需要文件夹(maxtocp)中的文件

边界是真的

if($ inboundprocess -eq $ true) {

# CHECK FOLDER EXISTED IF NOT CREATE 

if(-not(测试路径$ inboundFileDirectoryName))   {       新项目$ inboundFileDirectoryName-类型目录|空空   }

try 
{ 
        $logger.Info("INBOUND PROCESS Started");
        Write-Host "INBOUND PROCESS Started"
        [Microsoft.SharePoint.Client.ClientContext] $ctx=  NEW-OBJECT Microsoft.SharePoint.Client.ClientContext($inboundsiteUrl) ;
        $ctx.Credentials = New-Object System.Net.NetworkCredential($inboundUserName,$inboundUserPassword,$inbounduserDomain);
        [Microsoft.SharePoint.Client.Web]$web =$ctx.Web;
        [Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($inboundDocumentLibraryName);
        [Microsoft.SharePoint.Client.CamlQuery]$camlQery= New-Object  Microsoft.SharePoint.Client.CamlQuery;
        [Microsoft.SharePoint.Client.ListItemCollection] $listCollection = $list.GetItems($camlQery);
        $ctx.Load($web);
        $ctx.Load($list);
        $ctx.Load($listCollection);
        $ctx.ExecuteQuery();

        $logger.Info("INBOUND FILE COUNT " + $listCollection.Count.ToString());
         Write-Host "INBOUND FILE COUNT " + $list.ItemCount
        [System.Net.WebClient]$webclient = New-Object System.Net.WebClient
        $webclient.UseDefaultCredentials=$true; 
        #$webclient.Credentials = New-Object System.Net.NetworkCredential($inboundfdUserName,$inboundfdUserPassword,$inboundfduserDomain);  # $true  # need to modify this code with custom user credentials info

        foreach ( $lstitem in $listCollection)
        {

                try 
                { 
                      [Microsoft.SharePoint.Client.ListItem] $lstitem1 =$lstitem

                      if ($ctx.Url.LastIndexOf("/") = -1 ) 
                      {

                        $fileURL = $ctx.Url.TrimEnd('/') +  $lstitem1.FieldValues.FileRef
                        Write-Host $fileURL
                        $logger.Info("Downloading file " +  $fileURL);
                        Write-Host $fileURL  $lstitem1.FieldValues.FileRef
                        $outfile =   $lstitem1.Folder.FieldValues.FileLeafRef
                        Write-Host "Downloading file to"  $outfile
                        $webclient.DownloadFileAsync($fileURL,$outfile);
                        $logger.Info("File downloaded");
                        Write-Host "File downloaded"
                       }

                 }
                 catch [System.Exception] 
                    {

                          $logger.Error("error occcured while downloading file "  + $_.Exception.Message);
                           Write-Host "error occcured "  + $_.Exception.Message -ForegroundColor Red
                    }

        }
    }
    catch [System.Exception] 
    {

             $logger.Error("error occcured while reading URL and document library name "  + $_.Exception.Message);
             Write-Host "error occcured "  + $_.Exception.Message -ForegroundColor Red
    }

}

0 个答案:

没有答案