0

I am getting an Exception thrown when I try to run a script the first time after opening the PowerShell ISE but not the second time or anytime after that. It works everytime after the first run. I'm thinking it has something to do with the order I am calling things. Here is my code with some values replaced:

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$User = "[email protected]"
$pwFile = 'C:\encryptedPW.txt'
$PWord = (Get-Content $pwFile | ConvertTo-SecureString)
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

Connect-MsolService -Credential $creds
Connect-AzureAD -Credential $creds

$SiteURL="https://example.sharepoint.com/sites/exampleSite/"
$listName="Example List"
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName, $creds.Password)

$list=$Ctx.Web.Lists.GetByTitle($listName)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$Ctx.Load($items)
$Ctx.ExecuteQuery()

It throws the exception on the "$Ctx.ExecuteQuery()" line with the exception: Exception calling "ExecuteQuery" with "0" argument(s): "Value cannot be null. Parameter name: password"

1 Answer 1

0

Modify the code as below to to get list items from SharePoint online list.

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$User = "[email protected]"
$pwFile = 'C:\encryptedPW.txt'
$PWord=ConvertTo-SecureString (Get-Content $pwFile) -AsPlainText -Force

$SiteURL="https://example.sharepoint.com/sites/exampleSite/"
$listName="Example List"
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $PWord)

$list=$Ctx.Web.Lists.GetByTitle($listName)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$Ctx.Load($items)
$Ctx.ExecuteQuery()
foreach($item in $items){
    $item["Title"]
}
1
  • Hmm. That seems to not even authenticate. It seems to be something with the password but it's just weird that the first time I hit Run in the ISE it hits that exception but the next time and every time after that I hit Run it is fine. Unless I close the ISE and open it again hitting Run will hit that exception again but every time after that is fine. It must be storing something on that first Run to allow the second and every time after that to Run fine.
    – Nate Ham
    Sep 25, 2019 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.