How to put in condition to look up into a list of OneDrive URL in csv

JT 61 Reputation points
2021-02-15T04:21:54.917+00:00

67974-sourcecode.txt

  1. you can refer to the attached code.
  2. It work if I use this line

$srcUrl = "https://XXXXX.sharepoint.com/personal/xxxxxx_com"

3 But when I try to change above line into a looping condition, to look into a list of One Drive user's URLs like below code, then I will hit into error

$srcURLs=import-csv "C:\reports\list.csv"
write-host $srcURLs
Foreach($srcURL in $srcURLs)
{
write-host $srcURL
}

Error encountered

Authenticating ...
New-Object : Cannot find an overload for "ClientContext" and the argument count: "1".
At C:\reports\All item usage.ps1:196 char:15

  • ... rcContext = New-Object Microsoft.SharePoint.Client.ClientContext($src ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
  • FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

The property 'Credentials' cannot be found on this object. Verify that the property exists and can be set.
At C:\reports\All item usage_Manik_testCSV.ps1:198 char:1

  • $srcContext.Credentials = $credentials
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\reports\All item usage_Manik_testCSV.ps1:202 char:1

  • $srcList = $srcWeb.Lists.GetByTitle($srcLibrary)
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\reports\All item usage_Manik_testCSV.ps1:206 char:1

  • $listItems = $srcList.GetItems($query)
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\reports\All item usage.ps1:208 char:1

  • $srcContext.Load($srcList)
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\reports\All item usage_Manik.ps1:210 char:1

  • $srcContext.Load($listItems)
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\reports\All item usage_Manik.ps1:212 char:1

  • $srcContext.ExecuteQuery()
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

Total Count:
Total Count: Completed: 0
END Start : '15-Feb-21,12:11:43'
PS C:\reports>

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,969 questions
{count} votes

Accepted answer
  1. ChelseaWu-MSFT 6,331 Reputation points
    2021-02-15T07:42:44.803+00:00

    Please see the attachment for the modified script: 67965-modifiedcode.txt

    A few things you should notice:

    1 The rows in CSV file (with header srcURL) would be imported as collections and each object in the foreach loop would output as @{srcURL=https://XXXXX.sharepoint.com/personal/xxxxxx_com} instead of pure URL and that is what leads to the error.

    I am using Get-Content command to get the URLs without headers in CSV:

    $a = Get-Content -Path "C: \Reports\list.csv" | ForEach-Object {  
     write-host $_  
    }  
    

    Here is the reference: Get-Content.

    2 You will need to include the functions inside Foreach($srcURL in $srcURLs) to loop through $srcURLs. For example:

    $srcURLs = Import-Csv "C:\Reports\list.csv"   
    Foreach($_ in $srcURLs){  
     $srcLibrary = "Documents"  
     function WriteLog {  
     ...  
     }  
     function ScanFolders {  
     ...  
     }  
    }  
    

    3 Make sure you have access to all the personal OneDrive sites in the CSV file to avoid authentication issue.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    **Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. **


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.