I have a PS1 script that uses Microsoft.Online.SharePoint.PowerShell and downloads files from specific sharepoint library (Sharepoint Online).
It runs successfully on the SQL server machine thru SQL Server powershell, but fails when executed thru SQL Agent -> Powershell or cmdexec command with error: Exception calling "ExecuteQuery" with "0" argument(s): "Unable to connect to the remote server"
What could cause this error and how to troubleshoot this error?
Here is what PS1 script writes to log:
- Error connecting to library in SharePoint Online
- Exception calling "ExecuteQuery" with "0" argument(s): "Unable to connect to the remote server"
Here is what SQL Agent Job writes to log:
An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.. At E:\ Data ev\ga\ script\SPOnline. ps1:157 char:9 + $folder = $folders | Where {$_.Name -eq $FolderName} +
here is the piece of script (again, it runs fine on SQL Server powershell but fails when ran thru SQL Agent Job):
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
Try
{
#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)
$folders = $List.RootFolder.Folders
$Ctx.Load($folders)
$Ctx.ExecuteQuery()
Write-Log "Listed loaded."
}
Catch
{
Write-Log "Error connecting to library in SharePoint Online `n"
Write-Log $_.Exception.Message
}
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml= "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='TaskStatus' />
<Value Type='Text'>$TaskStatus</Value>
</Eq>
</Where>
</Query>
</View>"
$count =0
#Get List Items in Batches
Do
{
# $List.RootFolder.Folders[$FolderName]
if ($FolderName -ne "")
{
$folder = $folders | Where {$_.Name -eq $FolderName}
$ctx.load($folder)
$ctx.ExecuteQuery()
$Query.FolderServerRelativeUrl = $folder.ServerRelativeUrl
}
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$ListItems.Count
#Update Postion of the ListItemCollectionPosition
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
$Query.ListItemCollectionPosition
If ($ListItems.Count -eq 0)
{
Write-Log "No items in library that meets CAML query condition `n"
Break
}
$downloadItemCount=1;
#Extract the each list item from the List Items collection.
ForEach($Item in $ListItems)
{
try
{
$Ctx.Load($Item.File)
$Ctx.ExecuteQuery()
$SourceFile=$Item.File.ServerRelativeUrl;
#$Folder_Name1 = split-path $SourceFile
#$Folder_Name1 = split-path $Folder_Name1 -leaf;
#$TargetFile=$TargetFolder + "\"+$Folder_Name1+"\" + $Item.File.Name;
$TargetFile=$TargetFolder + "\" + $Item.File.Name;
If ($Action -ne "UpdateOnly")
{
FileDownLoadFromSPOnlineLibrary -SPOSiteURL $SiteURL -SourceFilePath $SourceFile -TargetFilePath $TargetFile
$fileDownloadingMessage="Downloaded: "+$Item.File.Name;
#Write-Host $fileDownloadingMessage -BackgroundColor DarkGreen
Write-Log $fileDownloadingMessage
}
$Item["TaskStatus"] = $UpdateStatus;
$Item[$DescriptionFieldname] = $UpdateDescription;
$Item.Update();
$Ctx.ExecuteQuery();
Write-Log "Set Task Status to Downloaded."
$downloadItemCount++
}
catch
{
$ErrorMessage = $_.Exception.Message +"in: " +$Item.File.Name
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage
}
}
Break; #just do 1 item.
}While ($Query.ListItemCollectionPosition -ne $null)