Wednesday, August 2, 2017

Install-O365Modules.ps1

Today's post is aimed at setting up your system to connect to all the components of O365.

I feel this script is pretty straight forward and self explaining. If receive requests will come back and do a more in depth post on what is happening.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<#
.SYNOPSIS
    Check for O365 components are present and attempt to install or direct to installers.
.DESCRIPTION
    Check for O365 components are present and attempt to install or direct to installers.
    Installs missing modules for:  
        o Active Directory Online
        o Skype for Business Online
        o SharePointOnline
.EXAMPLE
    Standard execution
    
    Install-O365Modules.ps1
.EXAMPLE
    Verbose messages for troubleshooting installation
    
    Install-O365Modules.ps1 -Verbose
.NOTES
    Created by Chris Lee
    Date April 20, 2017

    Some code pulled from:
        PoShConnectToOffice365.ps1
        Created by DJacobs for HBS.NET
.LINK
    GitHub: https://github.com/clee1107/Public/blob/master/O365/Install-O365Modules.ps1
    Blogger: http://www.myitresourcebook.com/2017/08/install-o365modulesps1.html

#>

[Cmdletbinding()]
Param
(
)

#################################
## DO NOT EDIT BELOW THIS LINE ##
#################################

## Install O365 Components
    ##Install MSOnline (Azure Active Directory)
        Write-Verbose -Message "Checking for MSOnline (Office 365) Module"
        ## Office 365
            Try 
                {
                    Write-Verbose -Message "Not connected to MSOnline (Azure Active Directory)"
                    Write-Verbose -Message "$(Get-Date -f o) Importing Module MSOline"
                    Import-Module MSOnline -DisableNameChecking -ErrorAction Stop
                }
            Catch 
                {
                    Write-Verbose -Message "MSOnline Module not found."
                    Write-Verbose -Message 'Check if PowerShell session running in "run as administrator"'
                    If (((whoami /all | select-string S-1-16-12288) -ne $null) -eq $false)
                        {
                            Write-Error 'PowerShell must be ran in "run as administrator to install MSOnline module"'
                            Exit
                        }
                    else 
                        {
                            Write-Host -ForegroundColor Yellow "Installing MSOnline Module"
                            Install-Module MSOnline
                            Try 
                                {
                                    Write-Verbose -Message "$(Get-Date -f o) Importing Module MSOline"
                                    Import-Module MSOnline -DisableNameChecking -ErrorAction Stop
                                }
                            Catch 
                                {
                                    Write-Error -Message "Error. Cannot import module 'MSOnline' because $_" -ErrorAction Stop
                                }
                        }
                    }
            
    ##Install SharePoint Online
        Write-Verbose -Message "Checking for SharePoint Online Module"
        ##SharePoint
                    Try 
                        {                   
                            Write-Verbose -Message "$(Get-Date -f o) Importing SharePoint Module"
                            Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking -ErrorAction Stop
                        }
                    Catch 
                        {
                            Write-Verbose -Message "SharePoint Online Module not found."
                            $temp = Read-Host "Launch browser to download SharePoint Online Management Shell? [Y]/N"
                            If ($temp -eq $null -OR $temp -eq "" -OR $temp -eq "Y")
                                {
                                    Start-Process https://www.microsoft.com/en-us/download/details.aspx?id=35588      
                                }
                            Else
                                {
                                    Write-Error -Message "Error. Failed to import the module 'Microsoft.Online.SharePoint.PowerShell' because $_" -ErrorAction Stop
                                }
                        }
                        
    ##Install Skype for Business
        Write-Verbose -Message "Checking for Skype for Business Module"
        Try 
            {
                Write-Verbose -Message "$(Get-Date -f o) Importing Module Skype for Business Online Connector"
                Import-Module SkypeOnlineConnector -DisableNameChecking -ErrorAction Stop
            }
        Catch 
            {
                Write-Verbose -Message "SkypeOnlineConnector Module not found."
                $temp = Read-Host "Launch browser to download Skype for Business Online, Windows PowerShell Module? [Y]/N"
                    If ($temp -eq $null -OR $temp -eq "" -OR $temp -eq "Y")
                        {
                            Start-Process https://www.microsoft.com/en-us/download/details.aspx?id=39366    
                        }
                    Else
                        {
                            Write-Error -Message "Error. Failed to import the module 'SkypeOnlineConnector' because $_" -ErrorAction Stop
                        }
            }

Full script can be accessed from following link:
https://github.com/clee1107/Public/blob/master/O365/Install-O365Modules.ps1
Further reference links for PowerShell cmdlets used can be found on following post:

Code Snippets created via: http://hilite.me/

No comments:

Post a Comment