Skip to content

Instantly share code, notes, and snippets.

@melvinpetix
Last active November 17, 2022 18:07
Show Gist options
  • Save melvinpetix/4956dc642a87384232eee61c75325eb4 to your computer and use it in GitHub Desktop.
Save melvinpetix/4956dc642a87384232eee61c75325eb4 to your computer and use it in GitHub Desktop.
<#
CreateTeamsRoomResource
- Melvin Biscarra
.DESCRIPTION
Create/Change Teams Meeting Room Resource
0 delete/Create Mailbox
1 Set Calendar defaults
2 Set UPN
3 Set Password to never expire
4 Assign License (Skipped if license parameter is $NULL)
5 Enable Skype/Teams
.EXAMPLE
.\CreateTeamsRoomResource.ps1 -step 0 -name mtrtest01 -displayname "MTR-TEST-01" -submittedby "adminekhl@nnit.com"
When set (step 0) it always means a new resource account needs to be created. It could either be because it is in fact a new meeting room, but it could also be that its an existing meeting room where an MTR device is installed in.
.EXAMPLE
.\CreateTeamsRoomResource.ps1 -step 1 -name mtrtest01 -Deletecomments $true -HiddenFromAddressListsEnabled $true
.\CreateTeamsRoomResource.ps1 -step 5 -name mtrtest01
Modify Existing Resource
.PARAMETERS
step, name, displayname, submittedby, Deletecomments (default $false), AutomateProcessing (default 'AutoAccept'), RemovePrivateProperty (default $false)
AddOrganizerToSubject (default $false), HiddenFromAddressListsEnabled (default $false), RoomLicence (default $null)
.NOTES
mtr.psm1 required (https://gist.github.com/melvinpetix/0e1488146addc9af84159ce58eddbc64)
#>
[CmdletBinding()]
param (
[int]$step,
[string]$name,
[string]$displayname,
[string]$submittedby,
[string]$Deletecomments = $false,
[string]$AutomateProcessing = 'AutoAccept',
[string]$RemovePrivateProperty = $false,
[string]$AddOrganizerToSubject = $false,
[string]$HiddenFromAddressListsEnabled = $false,
[string]$RoomLicence = $null
)
set-executionpolicy -scope CurrentUser -executionPolicy unrestricted -force
# REQUIRED!!!
Import-Module .\PSScript\mtr.psm1
Import-Module ExchangeOnlineManagement
Import-Module MSOnline
[string] $log
[string] $err
# Script Variables
$rmdomain = 'novonordisk.com'
$RoomUserPrincipalName = ($name + '@' + $rmdomain)
$submittedby = ($submittedby.substring($submittedby.length - 4, 4) + '@' + $rmdomain)
$newpassword = Random-Password -Length 15 -SpecialChar 1
try {
if($step -le 1){
GetCredential -type 'Office365'
$log += "$timestamp Connect-ExchangeOnline`r`n";
}
if($step -ge 2 ) {
Get-Credential -type 'MsOnline'
$log += "$timestamp Connect-MsolService`r`n";
}
if($step -ge 5) {
Get-Credential -type 'Skype'
$log += "$timestamp New-CSOnlineSession`r`n";
}
}
catch {
throw $_
}
try {
while($step -lt 5){
switch($step){
0 { #Remove/Create Existing Mailbox.
if(Get-Mailbox -Identity $name){
Get-Mailbox -Identity $name | Remove-Mailbox -PermanentlyDelete -ErrorAction Stop
$log += "$timestamp mailbox deleted.`r`n";
}
else {
$parmNewMailbox = @{
Name = $name
Alias = $displayname
Room = $true
EnableRoomMailboxAccount = $true
MicrosoftOnlineServicesID = $RoomUserPrincipalName
RoomMailboxPassword = (ConvertTo-SecureString -String $newpassword -AsPlainText -Force)
}
New-Mailbox @parmNewMailbox
}
while (-not (Get-Mailbox -Identity $name -ErrorAction SilentlyContinue)) {
Write-Host "Creating Mailbox.. Please wait"
Start-Sleep -Seconds 60
}
if($submittedby){
Send-Email -To $displayname -From Body 'Meeting Room $name password: $newpassword'
$log += "$timestamp Password sent`r`n";
}
$log += "$timestamp step $step. Mailbox Created`r`n";
step++
break
}
1 { #Set Calendar defaults unless specified in parameter input.
try{
$paramSetCalendarProcessing = @{
Identity = $name
AutomateProcessing = $AutomateProcessing
AddOrganizerToSubject = $AddOrganizerToSubject
DeleteComments = $DeleteComments
DeleteSubject = $false
RemovePrivateProperty = $RemovePrivateProperty
HiddenFromAddressListsEnabled = $HiddenFromAddressListsEnabled
}
Set-CalendarProcessing @paramSetCalendarProcessing
}
catch {
throw $_
}
log += "$timestamp Calendar setting completed.`r`n";
step++
break
}
2 { # Change UPN to novonordisk.com (default is novonordisk.onmicrosoft.com)
$msolUser = Get-MsolUser -UserPrincipalName "$RoomUserPrincipalName"
if ($msolUser -eq $null)
{
paramMsolUPN - @{
UserPrincipalName = ($name + '@novonordisk.onmicrosoft.com')
NewUserPrincipalName = $RoomUserPrincipalName
}
Set-MsolUserPrincipalName @$paramMsolUPN
}
else {
Write-Host "UPN = $RoomUserPrincipalName"
}
log += "$timestamp Set UPN complete.`r`n";
$step++
break
}
3 {
try {
$paramSetMsolUser = @{
UserPrincipalName = $RoomUserPrincipalName
PasswordNeverExpires = $true
}
Set-MsolUser @$paramSetMsolUser
}
catch {
throw $_
}
log += "$timestamp PasswordNeverExpires enabled.`r`n";
step++
break
}
4 { # Apply the License. Will not be evaluated if parameter $License is null.
if(-not($RoomLicence) -or ($RoomLicence -eq $null)){
$switch.MoveNext()
}
else {
$paramSetMsolUserLicense = @{
UserPrincipalName = $RoomUserPrincipalName
AddLicenses = $RoomLicence
}
Set-MsolUserLicense @paramSetMsolUserLicense
log += "$timestamp licensed assigned.`r`n";
}
$step++
break
}
5 { # Enable Skype for Business/Teams
try {
$paramEnableCsMeetingRoom = @{
Identity = $RoomUserPrincipalName
RegistrarPool = (Get-CsOnlineUser -Identity $CsOnlineUserTemplate | Select-Object -ExpandProperty RegistrarPool)
SipAddressType = 'EmailAddress'
}
Enable-CsMeetingRoom @paramEnableCsMeetingRoom
log += "$timestamp enabled skype/teams connector.`r`n";
step++
break;
}
catch {
throw $_
}
}
}
}
}
catch {
$log += "$timestamp Error: $PSItem.`r`n";
}
Finally {
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
Get-PsSession|Remove-PSSession;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment