通过Powershell脚本从Sharepoint下载文件时突然拒绝Acces [英] Suddenly Acces denied while downloading files from sharepoint via powershell script

查看:44
本文介绍了通过Powershell脚本从Sharepoint下载文件时突然拒绝Acces的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个脚本,用于将我的Sharepoint网站的.rdl报告副本下载到服务器上.到现在为止,它的运行情况一直很好.

I have a script I am running for downloading a copy of .rdl reports from my sharepoint site to my server. This has worked very fine until now.

我收到如下错误:

Blockquote 调用"OpenBinary"的异常;带有"0"参数:访问被拒绝. (除了来自HRESULT的信息:0x80070005(E_ACCESSDENIED))"在D:\ Installation_scripts \ test.ps1:21 char:1 + Invoke-Command -ComputerName serverXXXXX -Authentication CredSSP -cre den ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo:未指定:(:) [ ],MethodInvocationException + FullyQualifiedErrorId:UnauthorizedAccessException + PSComputerName:serverXXXXX

Blockquote Exception calling "OpenBinary" with "0" argument(s): "Access is denied. (Except ion from HRESULT: 0x80070005 (E_ACCESSDENIED))" At D:\Installation_scripts\test.ps1:21 char:1 + Invoke-Command -ComputerName serverXXXXX -Authentication CredSSP -cre den ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : UnauthorizedAccessException + PSComputerName : serverXXXXX

我检查了ULS日志和事件日志,但找不到任何相关内容.有人知道要检查什么或做什么?

I have checked ULS logs and Eventlog but cant find anything related. Someone have any idea what to check or what to do?

我的脚本如下:

#Keeps .ps1 script open after execution

 param ( $Show )
 if ( !$Show ) 
{
    PowerShell -NoExit -File $MyInvocation.MyCommand.Path 1
     return
 }

$Username = 'username'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

 Write-Host "Script running"

#Download all .rdl files/folders from /Reports
$ErrorActionPreference = "Stop"
 Invoke-Command -ComputerName hostxxx -Authentication CredSSP -credential $cred -ScriptBlock{
 Add-PSSnapin Microsoft.Sharepoint.Powershell | Out-Null
 $destination = "D:\\Backup\RDL_reports_script\Reports_$((get-date).toString('yyyyMMdd'))"
$webUrl = "hostxxx"
$listUrl = "hostxxx/Reports/Forms/current.aspx"
$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)

 if (Test-Path "$destination")
        {
        throw "Path already exist"              
        }

    else {

function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
    }
        #Download file
        $binary = $file.OpenBinary()
    $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
    $writer = New-Object System.IO.BinaryWriter($stream)
    $writer.write($binary)
    $writer.Close()
    }
}

 #Download root files
 ProcessFolder($list.RootFolder.Url)
#Download files in folders
 foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}
}
}
Write-Host  "Script executed"


非常感谢您提供有关如何解决此问题的帮助.

So help with how to proceed with this problem is much appreciated.

文森特(BR Vincent)

BR Vincent

推荐答案

您是否有权访问该文件?

Do you have permission to access the file?

请确保您有权访问所有文件.

Please make sure you have permission to access all files.

我建议您使用以下C#代码下载文件:

I suggest you use the following C# code to download file:

#region

       
/// <summary>

       
/// 下载文件

       
/// </summary>

       
/// <param
name="siteUrl"></param>

       
/// <param
name="ListName"></param>

       
/// <param name="fileName"></param>

       
public static void
downloadFile(string siteUrl, string
ListName, string fileName)

       
{

           
siteUrl = siteUrl.EndsWith("/") ?
siteUrl.Substring(0, siteUrl.Length - 1) : siteUrl;

           
ClientContext context = new ClientContext(siteUrl);

           
Web web = context.Site.RootWeb;

 

           
List source = web.Lists.GetByTitle(ListName);

           
context.Load(source);

           
context.ExecuteQuery();

 

           
FileCollection files = source.RootFolder.Files;

           
Microsoft.SharePoint.Client.File file =
files.GetByUrl(siteUrl + "/" + ListName + "/" +
fileName);

           
context.Load(file);

           
context.ExecuteQuery();

 

           
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context,
file.ServerRelativeUrl);

           
System.IO.Stream fileInputStream = fileInfo.Stream;

           
FileStream fileOutputStream = new FileStream("C:\\"+fileName,
FileMode.OpenOrCreate|FileMode.Append,
FileAccess.ReadWrite, FileShare.None);

           
byte[] bufferByte = new byte[1024
* 100];

 

           
int len = 0;

           
while ((len = fileInputStream.Read(bufferByte, 0,
bufferByte.Length)) > 0)

           
{

               
fileOutputStream.Write(bufferByte, 0, len);

               
fileOutputStream.Flush(true);

           
}

       
}

       
#endregion

 

最好的问候,

周卡尔


这篇关于通过Powershell脚本从Sharepoint下载文件时突然拒绝Acces的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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