functions/public/New-SMBOfficeDeployment.ps1

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
function New-SMBOfficeDeployment {
      <#
            .SYNOPSIS
            Provisions a set of groups/users in Office 365 based on an input CSV file
 
            .DESCRIPTION
            Adds a file name extension to a supplied name.
            Takes any strings for the file name or extension.
 
            .INPUTS
            None. You cannot pipe objects to New-SBSOfficeDeployment.
 
            .OUTPUTS
            System.Management.Automation.PSObject .
            New-SBSDeployment returns a custom PS object with information on the deployment:
            - Error information, if one occured
            - Provisioned users with sign-in information
            - Provisioned groups
 
            .EXAMPLE
            C:\PS> extension -name "File"
            File.txt
 
            .EXAMPLE
            C:\PS> extension -name "File" -extension "doc"
            File.doc
 
            .EXAMPLE
            C:\PS> extension "File" "doc"
            File.doc
 
            .LINK
            http://www.fabrikam.com/extension.html
 
            .LINK
            Set-Item
            #>


    [cmdletbinding(DefaultParameterSetName="TenantId")]
    [OutputType([psobject])]
    param(
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        # The location of the input CSV file.
        [String] $CSV,
        [parameter(ParameterSetName="TenantId",Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $TenantId,
        [parameter(ParameterSetName="TenantDomain",Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $TenantDomain,
        [parameter()]
        [ValidateNotNullOrEmpty()]
        [string] $DefaultPassword = $(New-SWRandomPassword),
         [parameter(,Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [pscredential] $Credential = (Get-Credential -Message "Please provide your Partner Credentials"),
        [Parameter(DontShow)]
        [ValidateNotNullOrEmpty()]
        [object] $SyncHash
    )
    
    begin{
        if($script:Log -eq $null){
            Start-Log
        }

         try{
             $DeploymentJob = new-object psobject -Property @{
                 Type="Office"
                 Duration = "00:00:00"
                 Completed = $false
                 Status = @{
                    ProvisionedUsers = @()
                    ProvisionedGroups = @()
                 }
                 Error=$null  
             }
            if($SyncHash){
                $SyncHash.DeploymentJob = ([ref]$DeploymentJob).value
            }
            $DeploymentStart = get-date
            $null = Connect-MsolService -Credential $Credential
            if($TenantDomain){
                $TenantId = ((Get-MsolPartnerContract -all).where{$_.DefaultDomainName -eq $TenantDomain}).TenantId
            }
            elseif($TenantId){$TenantDomain = ((Get-MsolPartnerContract).where{$_.TenantId -eq $TenantId}).DefaultDomainName}
            else{
                Write-Error -Type Error -Message "Invalid or missing tenant information provided"
                return $null
            }
            $DefaultDomain = (Get-MsolDomain|where{$_.IsDefault -eq $true}).Name
            write-log "Using default domain '$DefaultDomain' as mail suffix"
            $PSDefaultParameterValues = @{"*-MSOL*:TenantId"=$TenantId}
            write-log "Getting available licenses from O365"
            $Licenses = Get-O365License -TenantId $TenantId
            write-log "Creating CSP admin account"
            $CSPAdminCredential = new-cspadmin -DomainName $TenantDomain

          
           
         } catch {
             Write-Log -Type Error -Message "Error during Office 365 Connection: $_"
         }
    }
       
        
    
    process{

        try {
            write-log "Generating O365 Deployment Inventory"
            $Inventory = ConvertTo-O365 -Path $CSV -Licenses $Licenses -Separator ','
            ##### Exchange Connection
            write-log "Opening remote session to Exchange online"
            $Connect = $false
            while($Connect -eq $false){
                try{
                    $Session = Connect-O365 -Credential $CSPAdminCredential
                    $Connect = $true
                } catch {
                    if($_.Exception.Message -like "*403*"){
                        write-log "CSP Admin Account not ready yet. Retrying Connection in 60 seconds"
                        start-sleep -Seconds 60
                    } else {throw $_}
                }
            }
            write-log -type verbose -message "Connection succeeded. Importing Exchange Online PS Session."
            #####
            
            write-log -type information -message "Provisioning Users"
            $OneDriveUsers = @()
            $i = 0
            foreach($User in $Inventory.Users){
                $i++
                Write-Progress -Id 1 -Activity "Deploying O365 Solution"`
                 -Status "Provisioning Users "`
                 -CurrentOperation "$i/$($Inventory.Users.Count)"`
                 -PercentComplete (($i/$($Inventory.Users.Count))*100)
                $UserParameters = @{
                    Username=[Regex]::Replace("$($User.First).$($User.Last)@$($DefaultDomain)",'[^a-zA-Z0-9\@\.]', '')
                    FirstName=$User.First
                    LastName=$User.Last
                    Title=$User.Title
                    Password=$DefaultPassword
                    License=$User.License.Id
                    MobilePhone=$User.Mobile
                    Country=$User.Country
                }
                $ReturnUser = New-O365User @UserParameters
                $User.Login = $ReturnUser.SignInName
                $User.Password = $DefaultPassword
                $Inventory.Groups|where{$_.Owner -eq $User}|foreach{$_.Owner = $User}

                $DeploymentJob.Status.ProvisionedUsers += $User
                $OneDriveUsers += $UserParameters.UserName
                
            }
            Write-Progress -Id 1 -Completed -Activity "Deploying 0365 Solution"
            #write-log "Waiting some time to allow the user mailboxes to provision"
            #Start-Sleep -Seconds 30

            write-log -type information -message "Provisioning Groups"
            $i = 1
            foreach($Group in $Inventory.Groups){
                Write-Progress -Activity "Deploying O365 Solution"`
                -Status "Provisioning Groups"`
                -CurrentOperation "$i/$($Inventory.Groups.Count)"`
                -PercentComplete (($i/$($Inventory.Groups.Count))*100)
                $null = New-O365Group -GroupName $Group.Name -Type office -owner ($Group.Owner.Login.split('@')[0])
                $DeploymentJob.Status.ProvisionedGroups += $Group
                $i++
            }
            write-log "Populating Group Memberships"
            foreach($User in $Inventory.Users){
                foreach($Group in $User.Groups){
                    if($Group.Owner.Login -ne $User.Login){
                        $null = Add-O365UserToGroup -UserName $User.Login -GroupName $Group.Name -Type Office
                    } else {
                        write-log -type verbose -message "$User is owner of the group $Group, membership creation skipped"
                    }
                }
            }

            write-log -type information -message "Provisioning Onedrives"
            $OnMicrosoftDomain = ((Get-MsolDomain).where{$_.Name -like "*onmicrosoft.com"}).Name
            $TenantName = $OnMicrosoftDomain.split(".")[0]
            $TenantSPAdminUrl = "https://$($TenantName)-admin.sharepoint.com"
            write-log -message "Using $TenantSPAdminUrl as admin url for SP online" -type verbose
            $null = Initialize-O365OneDrive -Users $OneDriveUsers -Credential $CSPAdminCredential -SPOAdminUrl $TenantSPAdminUrl
            
            
           
           
               

        } catch {
            $DeploymentJob.Error = $_
            write-log -type error -message "$_ @ $($_.InvocationInfo.ScriptLineNumber) - $($_.InvocationInfo.Line))"  
        }
        finally{
            get-pssession|Remove-PSSession
            $DeploymentEnd = get-date
            $DeploymentDuration = New-TimeSpan -Start $DeploymentStart -End $DeploymentEnd
            $DeploymentJob.Duration = $("{0:HH:mm:ss}" -f ([datetime]$DeploymentDuration.Ticks))
            $DeploymentJob.Completed = $true
            ([ref]$DeploymentJob).Value
        }
    
    }
    end{
        write-log -message "Deployment Complete"
    }
}