Passing Array Variable from CSV to Identity for Get-SPOSite

Welcome Forums General PowerShell Q&A Passing Array Variable from CSV to Identity for Get-SPOSite

Viewing 2 reply threads
  • Author
    Posts
    • #194450
      Participant
      Topics: 2
      Replies: 2
      Points: 51
      Rank: Member

      Good Morning Everyone,

      I'm currently trying to find out information about specific user's one drive folders.

      If I test with just my username, it works just fine.  When I attempt to pass the variable through from a CSV file, it keeps the array tags around the variable, and fails because that website is not found.

      This works:

      Get-SPOSite -Identity https://domain-my.sharepoint.com/personal/0240357_domain_com | Select-Object Title, Owner, StorageUsageCurrent, Url
      
      Title          Owner                  StorageUsage	Current Url
      -----          -----                  ------------------- 	---
      Martin, R 	0240357@domain.com		61 	https://domain-my.sharepoint.com/personal/0240357_doma...

      These two options below do not work.

      $users = import-csv C:\scripts\names.csv
      foreach ($user in $users){Get-SPOSite -Identity https://domain-my.sharepoint.com/personal/"$user"_domain_com | Select-Object Title, Owner, StorageUsageCurrent, Url}

      or

      import-csv C:\Scripts\names.csv | ForEach-Object{Get-SPOSite -Identity https://domain-my.sharepoint.com/personal/"$user"_domain_com | Select-Object Title, Owner, StorageUsageCurrent, Url}

      Both of the above one liners will give me the same error:

      Get-SPOSite : Cannot get site https://domain-my.sharepoint.com/personal/@{User=0240357}_domain_com.
      At line:1 char:56
      + ... Each-Object{Get-SPOSite -Identity https://domain-my.sharepoint.co ...
      +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Get-SPOSite], ServerException
      + FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,Microsoft.Online.SharePoint.PowerShell.GetSite

      Is there anyway for me to pass that variable and also strip off the array tags so that it sits nicely inside the URL like I need it to?

       

      Thank you for your time,

      Rob Martin

    • #194456
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      Helping Hand
      Rank: Community Hero

      The for loop is references $User, which is basically the row that is being processed, so you need to tell it what Property (i.e. Column) you want. With the error message, it appears to be User, so it would $user.User:

      $users = import-csv C:\scripts\names.csv
      
      foreach ($user in $users) {
      
           $Identity = "https://domain-my.sharepoint.com/personal/$($user.User)_domain_com"
           #or
           $Identity = 'https://domain-my.sharepoint.com/personal/{0}_domain_com' -f $user.User
           #or
           $Identity = 'https://domain-my.sharepoint.com/personal/{0}_domain_com' -f ($user | Select -ExpandProperty User)
      
          
          Get-SPOSite -Identity $Identity | 
          Select-Object Title, Owner, StorageUsageCurrent, Url
      }
      
    • #194495
      Participant
      Topics: 2
      Replies: 2
      Points: 51
      Rank: Member

      Thank you Rob,

      This worked just like I needed it to.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.