
Create and manage resource in Office 365
# *
# * Script Name: Create and manage resources.ps1
# * Written by: Stephen Wireman
# * Date: May 24th, 2017
# * Version: 1.0.0
# * Prerequisites: Place script in the path c:\scripts\Powershell
# * Resource file RoomsList.csv must exist in the same directory
# * Will require the password for the account listed in $UserName variable
# * Install the following
# * AdministrationConfig_3.msi
# * msoidcli_64.msi
# *
# * Create an Office365 connection/session and import the MS Online commandlets
# *
# *******************************************************************************************************
If ($Session -eq $null) {
$UserName = "" #Account used to access onmicrosoft.com
$ConnectURL = "" #"https://FQDN Exchange Server Hostname/PowerShell/
$Cred = Get-Credential $UserName
$Session = New-PSSession -ConfigurationName Microso ft.Exchange '
-ConnectionUri https://ps.outlook.com/powershell -Credential $Cred -Authentication Basic -AllowRedirection'
$ImportResults = Import-PSSession $Session -AllowClobber
Import-Module MSOnline -Verbose
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectURL -Authentication Kerberos
Import-PSSession -Session $Session -AllowClobber
Import-Module ActiveDirectory -Verbose
Connect-MSOLService -Credential $Cred
}
# Create a log file
$outFile="Create-and-set-Rooms{0:yyyyMMdd-HHmm}.log" -f (Get-Date)
$currentUser = [Environment]::UserName
$header = "<!#" + $(Get-Date) + "><Rooms Modfied by $currentUser logname: $outFile>"
Out-File -FilePath $outfile -InputObject $header
Import-Csv ".\Roomnames.csv" | foreach {
# Create Active Directory account
$lineOut = "<!#" + $(Get-Date) + "#!><Creating Active Directory account for " + $_.strName + "<!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
Try {
Write-Host "Covert password to secure string"
$ConvertedPassword = (ConvertTo-SecureString $_.strPass -AsPlainText -Force)
$lineOut = "<!#" + $(Get-Date) + "#!><Password was converted><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$lineOut = "<!#" + $(Get-Date) + "#!><ERROR:" + $FailedItem + ":" + $ErrorMessage + "><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
}
Try {
Write-Host "Creating Active Directory account for" $_.strName
New-ADUser -Name $_.strName -SamAccountName $_.strName -DisplayName $_.strName -UserPrincipalName $_.strEmail '
-EmailAddress $_.strEmail -AccountPassword $ConvertedPassword '
-Path 'OU=Resources,DC=stephenwireman,DC=com' -PassThru '
-Enabled $true | Out-File -FilePath $outfile -Append -NoClobber'
$lineOut = "<!#" + $(Get-Date) + "#!><Account was created><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
Start-Sleep 30
Try {
Write-Host "Performing mail enable action"
Enable-MailUser -Identity $_.strName -Alias $_.strName -ExternalEmailAddress $_.strExtEmail '
| Out-File -FilePath $outfile -Append -NoClobber'
Set-MailUser -Identity $_.strName -CustomAttribute15 CmgExcludeFromDirSync -UseMapiRichTextFormat Never '
-PrimarySmtpAddress $_.strEmail -WarningAction SilentlyContinue'
$lineOut = "<!#" + $(Get-Date) + "#!><Mail enable actions complete><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$lineOut = "<!#" + $(Get-Date) + "#!><ERROR:" + $FailedItem + ":" + $ErrorMessage + "><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
}
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$lineOut = "<!#" + $(Get-Date) + "#!><ERROR:" + $FailedItem + ":" + $ErrorMessage + "><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
}
Write-Host "All tasks completed for account " $_.strName
Write-Host " "
$lineOut = "<!#" + $(Get-Date) + "#!><All tasks completed for this account><!#" + $(Get-Date) + "#!>"
Out-File -FilePath $outfile -Append -NoClobber -InputObject $lineOut
}
# Open up the log file in Excel.
Invoke-Item $outFile
Remove-PSSession $Session
Magnificent beat ! I would like to apprentice whilst you amend your web site, how could i subscribe for a…
Some times its a pain in the ass to read what website owners wrote but this site is really user…
What’s up it’s me, I am also visiting this web site on a regular basis, this site is genuinely nice…
Some times its a pain in the ass to read what people wrote but this internet site is real user…
Hi there this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you…
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable…
Long time supporter, and thought I’d drop a comment. Your wordpress site is very sleek – hope you don’t mind…
Hello friends, good piece of writing and fastidious urging commented here, I am actually enjoying by these.
I have been browsing online more than 2 hours today, yet I never found any interesting article like yours. It’s…