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

Leave a Comment

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