Functions/Public/Get-OBNClient.ps1

function Get-OBNClient {

  <#
.SYNOPSIS
  Get Office 365 delegated customer information
 
.DESCRIPTION
  Get a list of Office 365 tenancies that the account used to connect has delegated
  admin access to.
 
.EXAMPLE
  Get-OBNClient
 
  Get a a list of Office 365 tenancies.
 
.EXAMPLE
  Get-OBNClient -Csv onebyte
 
  Get a a list of Office 365 tenancies. The list will be converted
  to CSV and saved in the file 'onebyte.csv' in the current directory.
 
.NOTES
  Authored By: Simon Buckner
  Email: simon@onebyte.net
  Date: 7th August 2020
 
.LINK
  https://github.com/simononebyte/PSOnebyte
 
#>


  [CmdletBinding(SupportsShouldProcess = $False)]
  Param(
    # Output to the specified CSV file
    [Parameter(
      Mandatory = $false,
      Position = 0
    )]
    [ValidateLength(1, 256)]
    [String]$CSV

  )

  BEGIN {

    # Run one-time set-up tasks here, like defining variables, etc.
    Set-StrictMode -Version Latest
    Write-Verbose -Message "$($MyInvocation.MyCommand.Name): Started."

    # Declare any classes used later in the sript
    # #########################################################################


    # Declare any supporting functions here
    # #########################################################################
    # function myFunc([string]$p) {
    # Write-Host "myFunc called with"
    # }


    # Run any initialisation steps here
    # #########################################################################
  }


  Process {

    $output = @()

    $contracts = Get-MsolPartnerContract -ErrorAction SilentlyContinue
    if ($?) {

      foreach ($contract in $contracts) {
        $tenantId = $contract.TenantId.Guid
        # Write-Output "$tenantId"
        $info = Get-MsolPartnerInformation -TenantId $tenantId -ErrorAction SilentlyContinue
        if ($?) {
          $name = $info.PartnerCompanyName

          $client = [OBNClient]::New()
          $client.TenantId = $tenantId
          $client.Name = $name
          $output += $client
        }
      }

      if ($CSV -eq "") {
        Write-Verbose "Sending results to the output pipe"
        $output
      }
      else {
        if ($CSV -notmatch "\.csv$") {
          $CSV = "$CSV.csv"
        }
        Write-Verbose "Sending results to CSV file $CSV"

        $output | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath $CSV
      }

    }
    else {
      Write-Error "Have you run Connect-Msol"
    }

  }

  END {
    # Finally, run one-time tear-down tasks here.
    Write-Verbose -Message "$($MyInvocation.MyCommand.Name): Complete."
  }

}