0

I am trying to download a single file from our teamsite (sharepoint?) using powershell..The reason for powershell is that any other method fails due to our policies (mapping, open in library, webDav access...)

I already tried several PS scripts I found here, or in web, but still nothing works...mostly I get errors like forbidden or invalid credentials...

the latest I am using:

function GetSharePointListContext($userName, $password, $isOnline, $siteUrl, $listTitle)
{
    $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
    if($isOnline -eq $true)
    {
        $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securePassword)  -Verbose:$false
    }
    else
    {
        if($userName -like "*@xyz.com")
        {
            $context.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
            $context.Credentials = New-Object System.Net.NetworkCredential($userName, $securePassword) -Verbose:$false  
            $context.add_ExecutingWebRequest( ${function:ExecutingWebRequestEventHandler_NoFormsAuth} )
        }
        else
        {
            $Context.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::FormsAuthentication
            $Context.FormsAuthenticationLoginInfo = New-Object Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo($userName, $securePassword) -Verbose:$false 
        }
    }

    $targetList = $Context.Web.Lists.GetByTitle($listTitle) #target list  

    $Context.Load($targetList)  
    $Context.ExecuteQuery()  

    return $targetList;
}    

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"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll' 
Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.PowerShell.dll'


$userName= ""
$password = ""
$isOnline = $false
$siteUrl = "https://hub.xyzgroup.net/teams/xyzteam/"
$listTitle = "Process Analysts Workplace"

GetSharePointListContext $userName $password $isOnline $siteUrl $listTitle

This will result in:

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden." At C:\Learning\PowerShell\test8.ps1:28 char:5 + $Context.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException

Any idea what is wrong? I realize this script is not to download a file, but that one results in the same error :/

Thanks.

sopis
  • 1
  • actually this is teamsite...this is the response from our sharepoint team, who gave us the script and have no more time to help... "The error message is valid. You are trying to access our External SharePoint environment, which is a SP2013 On-Premise environment. I guess you would like to access a Internal TeamSite (SP Online), therefore you would just have to change the siteUrl accordingly." – sopis Mar 15 '18 at 16:06
  • Is the credential AD account? – fdafadf Mar 15 '18 at 16:10
  • what do you mean exactly? the account I use is the one I use to login to sharepoint for example...but in the code I dont use the [domain]\[username] account - should I do that? – sopis Mar 15 '18 at 16:17
  • A 403 means whatever context the script was run under (for example, maybe a privileged or admin account you don't normally access Sharepoint with that has different permissions in AD than your regular account) that identity was denied access. Do you get a different result if you specify credentials in your code? – trebleCode Mar 15 '18 at 21:45

0 Answers0