Last active
November 18, 2022 17:34
-
-
Save melvinpetix/33aa3baf58a0361d939d968e5dca807a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Name : CreateMicrosoftTeamsRoomResource | |
#Syntax .\PsScript\CreateMicrosoftTeamsRoomResource.ps1 –step 0 –name TestExoMeetingRm15 -TaskSubmittedBy $NULL | |
param ( | |
[int] $step, | |
[string] $name, | |
[string] $displayName, | |
[string] $TaskSubmittedBy | |
) | |
set-executionpolicy -scope CurrentUser -executionPolicy unrestricted -force | |
Import-Module ActiveDirectory | |
Import-Module MsOnline | |
Import-Module .\PSScript\ExoLibNN.psm1 | |
[string] $log, $err | |
$resourceOu = "OU=NovoNordiskGen,OU=Company,$baseOU" | |
$timestamp = Get-Date –f "yyyy-MM-ddTHH:mm:ss"; | |
$displayName = $displayName -replace "'","" | |
if($TaskSubmittedBy -eq $null){ | |
$TaskSubmittedBy = "mebi@nnit.com" | |
} | |
try | |
{ | |
$Error.Clear(); | |
$pass = cat ".\PSScript\365secure.txt" | Convertto-securestring | |
$365CREDS = New-Object -typename System.Management.Automation.PsCredential -argumentlist autouserw2k@novonordisk.com, $pass | |
$UserCredential= New-Object System.Management.Automation.PsCredential("corp\autouserw2k",$pass) | |
$genPwd = New-RandomPassword -MinimumPasswordLength 10 -MaximumPasswordLength 12 -NumberOfAlphaNumericCharacters 0 | |
$rmPass = $genPwd | |
while ($step -le 1) | |
{ | |
switch ($step) | |
{ | |
0 { | |
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://NNEXMRDK101.corp.novocorp.net/powershell -Authentication Kerberos -Credential $UserCredential | |
Import-PSSession $Session -AllowClobber|Out-Null; | |
$rm = Get-RemoteMailbox -Identity "$name" -ErrorAction SilentlyContinue; | |
if ($rm -eq $null) | |
{ | |
$rmPass = $genPwd | |
$rmPass | Out-Null | |
try { | |
New-RemoteMailbox -Name $name -Room -DisplayName "$displayname" -SamAccountName $name -UserPrincipalName $name@novonordisk.com -OnPremisesOrganizationalUnit "$resourceOU" -ErrorAction Stop; | |
} catch { | |
throw $PSItem | |
} | |
} | |
$log += "$timestamp step $step. mailbox $name created.`r`n"; | |
$step++; | |
break; | |
} | |
1 { | |
try{ | |
Set-CalendarProcessing -Identity $name -AutomateProcessing AutoAccept -AddOrganizerToSubject $false -AllowConflicts $false -DeleteComments $false -DeleteSubject $false -RemovePrivateProperty $false -ErrorAction Stop; | |
} catch { | |
throw $PSItem | |
} | |
$step++; | |
$log += "$timestamp step $step. Completed"; | |
break; | |
} | |
2 { | |
Connect-MsolService -Credential $365CREDS -ErrorAction Stop; | |
$MsolLicense = Get-MsolAccountSku | Where-Object -like "MTR_Device_License_Management" | |
if ($MsolLicense -ne $null) { | |
try { | |
Set-MsolUser -UserPrincipalName $name -PasswordNeverExpires $true; | |
Set-MsolUserLicense -UserPrincipalName $name -AddLicenses $MsolLicense.AccountSkuId | |
} | |
catch { | |
throw $_ | |
} | |
} | |
$step++; | |
break; | |
} | |
3 { | |
try { | |
$tm = Get-RemoteMailbox -Identity "$taskSubmittedBy" -ErrorAction SilentlyContinue | |
if ($tm -eq $null) | |
{ | |
Send-MailMessage -SmtpServer mailrelay.novonordisk.com -To $TaskSubmittedBy -From noreply@novonordisk.com -Subject "Conf room $name created" -Body "Password: $rmPass" | |
} | |
} | |
catch { | |
throw $_ | |
} | |
$log += "$timestamp step $step. Completed"; | |
break; | |
} | |
} | |
$step ++; | |
} | |
} | |
catch | |
{ | |
$log += "$timestamp step $step. Error: $PSItem.`r`n"; | |
} | |
Finally | |
{ | |
Write-Output $log; | |
Get-PsSession|Remove-PSSession; | |
if ($step -ge 4) | |
{ | |
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue | |
} | |
} | |
function New-RandomPassword { | |
param( | |
[Parameter()] | |
[int]$MinimumPasswordLength = 5, | |
[Parameter()] | |
[int]$MaximumPasswordLength = 10, | |
[Parameter()] | |
[int]$NumberOfAlphaNumericCharacters = 0, | |
[Parameter()] | |
[switch]$ConvertToSecureString | |
) | |
Add-Type -AssemblyName 'System.Web' | |
$length = Get-Random -Minimum $MinimumPasswordLength -Maximum $MaximumPasswordLength | |
$password = [System.Web.Security.Membership]::GeneratePassword($length,$NumberOfAlphaNumericCharacters) | |
$password | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment