Archive/Connect-Office365.ps1


function Connect-Office365 {

    # version 1

    [CmdletBinding()]
    param(
        [Parameter(Position = 0)]
        [ArgumentCompleter( {
                param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
                (Get-ChildItem -Path "$([Environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\pwd\$wordToComplete*" -Directory).Name
            } )]
        [string]
        $Tenant,

        [Parameter()] [string[]] [ValidateSet("ExchangeOnline", "SharePoint")] $ExtraServices

    )



    $PwdFolder = "$([Environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\pwd\" + $Tenant

    #$OnpremPasswordFile = $PwdFolder + "\Onprempwd.xml"
    $Office365PasswordFile = $PwdFolder + "\Office365pwd.xml"


    #Set-Variable -Name $ONPREMCREDS -Value (Get-Credential -UserName AMARIS\admin_beheer -Message "Enter password for Domain Admin") -Scope Global

    #$Global:ONPREMCREDS = Get-Credential -UserName AMARIS\admin_beheer -Message "Enter password for Domain Admin"
    if ($Tenant) {
        #$Global:ONPREMCREDS = New-Object -typename System.Management.Automation.PSCredential -argumentlist "AMARIS\admin_beheer", (Get-Content $OnpremPasswordFile | ConvertTo-SecureString)
        #$Global:UserCredential = New-Object -typename System.Management.Automation.PSCredential -argumentlist "admin_beheer@amariszorggroep.onmicrosoft.com", (Get-Content $Office365PasswordFile | ConvertTo-SecureString)
        $Global:OfficeCredential = Import-Clixml -Path $Office365PasswordFile
    }
    <#else {
        $Global:ONPREMCREDS = Get-Credential -Message "Enter password for Domain Admin"
        $Global:OfficeCredential = Get-Credential -Message "Enter password for Office365 Admin"
    }#>




    # MSOL, AzureAD and MSGraph connections

    Connect-AzureAD -Credential $OfficeCredential -ErrorAction Stop

    Connect-MsolService -Credential $OfficeCredential

    <#
    if ($Tenant) {
        Connect-MSGraph -Credential $OfficeCredential
    }
    else {
        Connect-MSGraph
    }
    #>




    ### Get SPOadmin URL
    # $SPOSite = @{}

    if ($ExtraServices -contains "SharePoint") {
        switch ($tenant) {
            "RAM" {
                $SPOURL = "https://ramit-admin.sharepoint.com/"
            }
        }
        if (!$SPOURL) {
            $SPOSite_AzureADTenantDetail = Get-AzureADTenantDetail
            $SPOSite_TenantOnmicrosoftDomain = $SPOSite_AzureADTenantDetail | Select-Object -ExpandProperty VerifiedDomains | Where-Object { $_.Name -like "*.onmicrosoft.com" -and $_.Name -notlike "*.mail.*" }
            $SPOSite_ExplodedDomainname = ($SPOSite_TenantOnmicrosoftDomain.Name).Split('.')

            if ($SPOSite_ExplodedDomainname[0]) {
                $SPOURL = "https://$($SPOSite_ExplodedDomainname[0])-admin.sharepoint.com/"
            }
        }
        if ($SPOURL) {
            try {
                Connect-SPOService -Url $SPOURL -Credential $OfficeCredential -ea 0
            } catch {
            } finally {
                $SPOURL
            }
        }
    }




    if ($ExtraServices -contains "ExchangeOnline") {
        Connect-ExchangeOnline -Credential $OfficeCredential # -ShowBanner $false
        #$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $OfficeCredential -Authentication Basic -AllowRedirection
        #Import-PSSession $Session
    }



    if ($Tenant) {
        $host.ui.RawUI.WindowTitle = $Tenant
    } else {
        # $host.ui.RawUI.WindowTitle = Read-Host -Prompt "Window Title"
        $host.ui.RawUI.WindowTitle = (Get-AzureADTenantDetail).DisplayName
    }


}