none
Get SharePoint Online Site Collection Admins RRS feed

  • Question

  • Hi,

    Wondering if anyone can help. 

    When i run the below script data is exported to a CSV. I wan't all of the data to appear in columns, this is possible for $secadmins outputs but $sitecoll.url seems to be in the same column as $secadmins.

    $adminUPN="_______@_____"
    $SiteURL = "https://_____-admin.sharepoint.com"
    $userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."
    Connect-SPOService -Url $SiteURL -Credential $userCredential

    #Set file location for saving information. We'll create a tab separated file.
    $FileLocation = "SiteCollectionOwnersReport.csv"
    "Site Collection Admin Report" | Out-file $FileLocation
    $sitecolls = get-sposite -limit all

    foreach ($sitecoll in $sitecolls)
    {
    $sitecoll.url 
    $sitecoll.url | Out-file -Append $FileLocation
    if ($sitecoll.url -ne 'https://_______.sharepoint.com/')
    {
    $secadmins = Get-SPOUser -Site $sitecoll.url -Limit All | where {$_.IsSiteAdmin} 
    $secadmins | Out-file -Append $FileLocation
    }
    }

    Write-Output "Script Execution finished"
        
    ##############################################################################
    ## End of Script
    ##############################################################################


    Callum

    Tuesday, August 13, 2019 9:23 AM

Answers

  • FYI - i resolved this

    New-Object -TypeName PSCustomObject -Property @{
    URL = $sitecoll.url
    DisplayName = $secadmins.DisplayName
    LoginName = $secadmins.LoginName
    Groups = $secadmins.Groups
    } | Select-Object URL, @{n='DisplayName';e={$_.DisplayName[0.. ($_.DisplayName.count-1)] -join ","}}, @{n='Groups';e={$_.Groups[0.. ($_.Groups.count-1)] -join ","}}, @{n='LoginName';e={$_.LoginName[0.. ($_.LoginName.count-1)] -join ","}} |Export-Csv -Path NameOfFile.csv -NoTypeInformation -Append
    }
          }


    Callum

    • Marked as answer by Callum Moore Tuesday, August 13, 2019 9:52 AM
    Tuesday, August 13, 2019 9:52 AM

All replies

  • FYi - i believe i've resolved this by amending with the below. But it seems LoginName and DisplayName returns with System.Object[]. Does anyone know how to resolve this? 

    foreach ($sitecoll in $sitecolls)
    {
    $sitecoll.url 
    #$sitecoll.url | Out-file -Append $FileLocation
    if ($sitecoll.url -ne 'https://_______.sharepoint.com/')
    {
    $secadmins = Get-SPOUser -Site $sitecoll.url -Limit All | where {$_.IsSiteAdmin} 
    #$secadmins | Out-file -Append $FileLocation

    New-Object -TypeName PSCustomObject -Property @{
    URL = $sitecoll.url
    DisplayName = $secadmins.DisplayName
    LoginName = $secadmins.LoginName
    } | Export-Csv -Path NameOfFile.csv -NoTypeInformation -Append
    }
          }


    Callum

    Tuesday, August 13, 2019 9:32 AM
  • FYI - i resolved this

    New-Object -TypeName PSCustomObject -Property @{
    URL = $sitecoll.url
    DisplayName = $secadmins.DisplayName
    LoginName = $secadmins.LoginName
    Groups = $secadmins.Groups
    } | Select-Object URL, @{n='DisplayName';e={$_.DisplayName[0.. ($_.DisplayName.count-1)] -join ","}}, @{n='Groups';e={$_.Groups[0.. ($_.Groups.count-1)] -join ","}}, @{n='LoginName';e={$_.LoginName[0.. ($_.LoginName.count-1)] -join ","}} |Export-Csv -Path NameOfFile.csv -NoTypeInformation -Append
    }
          }


    Callum

    • Marked as answer by Callum Moore Tuesday, August 13, 2019 9:52 AM
    Tuesday, August 13, 2019 9:52 AM
  • Hi Callum, 

    Thanks for sharing.

    Your solution will be beneficial to others in the community who meet this similar issue in the future. 

    Have a nice day.

    Best Regards, 

    Lisa Chen


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, August 14, 2019 1:39 AM
    Moderator