Create and manage resource in Office 365
Spread the love
# *
# * 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=corp,DC=ent,DC=metrohealth,DC=org' -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

This article has 8 comments

  1. Justin Reply

    Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me
    asking what theme you’re using? (and don’t mind if I steal
    it? :P)

    I just launched my site –also built in wordpress like yours– but
    the theme slows (!) the site down quite a bit.

    In case you have a minute, you can find it by searching for “royal cbd” on Google (would appreciate any feedback) – it’s still in the works.

    Keep up the good work– and hope you all take care of yourself during the coronavirus scare!

  2. clubriders.men Reply

    Thank you for the good writeup. It in fact was a amusement account it.

    Look advanced to far added agreeable from you! By the way,
    how can we communicate?

  3. Danny Reply

    Hi there this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to
    manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  4. Rosalina Reply

    Some times its a pain in the ass to read what people wrote but this internet site is real
    user friendly!

  5. den ngu de ban de thuong Reply

    Magnificent beat ! I would like to apprentice whilst you amend your web site, how could i subscribe for a
    weblog web site? The account helped me a appropriate deal.
    I had been tiny bit acquainted of this your broadcast offered shiny clear concept

  6. Laurel Reply

    I have been browsing online more than 2 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my opinion, if all webmasters
    and bloggers made good content as you did, the web will be much more useful than ever before.

Leave a Reply to Laurel Cancel reply

Your email address will not be published. Required fields are marked *