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.