Hello365.ps1

This is my script for connecting to Office 365. It features a multiple chose option to allow quick swapping between different tenants, and a custom command prompt that displays the tenant name and account used to connect.

Right now this script assumes that you have the standard modules for Azure, SharePoint Online and Skype for Business.  If you do not, just add a # in front of the line requesting it. I be improving module handling in a future version of this script.

Please feel free to ask questions in the comments below.

 

 ############################
##                          ##
##    Hello365  (<WSC>)     ##
##                          ##
 ############################

# Removing previous sessions
Get-PSSession | Remove-PSSession

cls
# get-ExecutionPolicy
# Set-ExecutionPolicy RemoteSigned

#  Tenant selector
do {
  do {
        write-host "Choose a Tenant"
        write-host "W - WSC"
        write-host "B - Bos365 User Group"
        write-host "S - not in use"
        write-host "D - Demo Data"
        write-host ""
        write-host "X - Exit"
        write-host ""
        write-host -nonewline "Type your choice and press Enter: "
        $choice = read-host
        write-host ""
    $ok = @("W","B","S","D","X") -contains $choice
    if ( -not $ok) { write-host "Invalid selection" }
  }
  until ( $ok )
  switch ( $choice ) {
    "W" {
        cls
        write-host "You entered 'WSC'"
        write-host "This is a Production tenant!!"
        $AdminAcct = "Mike@WSC.com"
        $Tenant = "WSC"
        break
    }
    "B" {
        cls
        write-host "You entered 'Bos365'"
        write-host "This is a Production tenant!!"
        $AdminAcct = "powershell.admin@BostonO365UserGroup.com"
        $Tenant = "Bos365"
        break
    }
    "S" {
      cls
      write-host "not in use"
      break
    }
    "D" {
        cls
        write-host "You entered 'Demo'"
        $AdminAcct = "miked@WSC2013.onmicrosoft.com"
        $Tenant = "WSC2013"
        break
    }
    "x" {
        cls
        write-host "Exiting"
        $AdminAcct = ""
        $Tenant = ""
        $LiveCred = ""
        Get-PSSession | Remove-PSSession
        exit
        break
    }
  }
}
until ( $ok )


# Connecting
$LiveCred = Get-Credential -Message "o365 Tenant Admin" -UserName $AdminAcct
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $LiveCred -Authentication Basic -AllowRedirection
$today = "{0:MM-dd-yy}" -f (Get-Date)

write-host "Today is " $today

# building a custom prompt
function prompt
{
    Write-Host ("PS " + $(Get-Date -f {M.dd.yy h:mm tt}) +" \") -foregroundcolor White
    Write-Host ("PS " + "Logged on to: " + $Tenant + " as " + $AdminAcct.split('@')[0] + " \") -foregroundcolor White
    Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta 
    return " "
}


#sessions
Import-PSSession $Session -AllowClobber
Connect-MsolService -Credential $LiveCred
# $session = New-CsOnlineSession -Credential $LiveCred

# quick test to verify we connected properly and to the correct tenant
Get-Mailbox -ResultSize 10 | ft

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Import-Module LyncOnlineConnector
Connect-SPOService -Url https://$tenant-admin.sharepoint.com -credential $LiveCred



# Get-Command -Module  Microsoft.Online.SharePoint.PowerShell
# get-module -ListAvailable
# get-module -ListAvailable | Import-Module


# new path
# New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $livecred -Authentication Basic -AllowRedirection

# old path
# New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
# rev 2.0

 

Leave a Reply

Your email address will not be published. Required fields are marked *