$ErrorActionPreference = "Stop" $MaximumFunctionCount = 8192 function ImportModules([parameter(mandatory)][String]$moduleName, [parameter(mandatory)][String]$requiredVersion) { # Check if the module is already loaded $loadedModule = Get-Module -Name $moduleName -ListAvailable -verbose if ($loadedModule) { Write-Host "$moduleName module is already loaded." -ForegroundColor Green } else { # Check if the module is installed Write-Progress "Importing modules" Write-Progress "Checking if $moduleName is installed" $installedModule = Get-InstalledModule $moduleName -ErrorAction SilentlyContinue if (!$installedModule) { Write-Progress "$moduleName module is not installed." Write-Progress "Installing $moduleName Module" Write-Host "Installing $moduleName Module..." -ForegroundColor Green Install-Module $moduleName -RequiredVersion $requiredVersion -Confirm:$false -Force Write-Progress "$moduleName Module installed successfully." } else { $stringVersion = (Get-InstalledModule $moduleName).Version.ToString() Write-Progress "$moduleName module is already installed with the version " Write-Progress $stringVersion if (!($stringVersion -eq $requiredVersion)) { Write-Host "Module version is different from $requiredVersion. Installing the $requiredVersion version" Write-Host "Installing $moduleName $requiredVersion Module..." -ForegroundColor Green Install-Module $moduleName -RequiredVersion $requiredVersion -Confirm:$false -Force Write-Host "$moduleName Module installed successfully." } else { Write-Progress "$moduleName Module Version $requiredVersion is already installed." -Completed } } } } function ImportCloudMModules ([String]$WorkFolder, [bool]$limitedScope) { Set-Location -Path $workFolder ImportModules -moduleName Microsoft.Graph.Identity.DirectoryManagement -requiredVersion 2.10.0 ImportModules -moduleName Microsoft.Graph.Applications -requiredVersion 2.10.0 if ($limitedScope) { ImportModules -moduleName Microsoft.Graph.Users -requiredVersion 2.10.0 ImportModules -moduleName Microsoft.Graph.Files -requiredVersion 2.10.0 ImportModules -moduleName Microsoft.Graph.Sites -requiredVersion 2.10.0 ImportModules -moduleName Microsoft.Graph.Groups -requiredVersion 2.10.0 ImportModules -moduleName Microsoft.Graph.Teams -requiredVersion 2.10.0 ImportModules -moduleName ExchangeOnlineManagement -requiredVersion 3.2.0 } $retryPms1 = Join-Path -Path $WorkFolder -ChildPath "CloudM-Retry.psm1" if (!(Test-Path -Path $retryPms1 -PathType Leaf)) { throw (New-Object System.IO.FileNotFoundException("File not found: $retryPms1", $retryPms1)) } else { Import-Module .\CloudM-Retry -Force } if ($limitedScope) { $processEmailDrive = Join-Path -Path $WorkFolder -ChildPath "CloudM-ProcessCsvs.psm1" if (!(Test-Path -Path $processEmailDrive -PathType Leaf)) { throw (New-Object System.IO.FileNotFoundException("File not found: $processEmailDrive", $processEmailDrive)) } else { Import-Module .\CloudM-ProcessCsvs -Force } } } function MoveFiles([parameter(mandatory)][String]$sourceFolder, [parameter(mandatory)][String]$ClientAppId) { $destinationPath = Join-Path -Path $workFolder -ChildPath "CloudM-Limited-App-$($ClientAppId)" New-Item -ItemType Directory -Path $destinationPath -Force | Out-Null $file = Join-Path -Path $workFolder -ChildPath "EmailDrive.csv" if ((Test-Path -Path $file -PathType Leaf)) { $newFile = "$($destinationPath)\EmailDrive - $($ClientAppId) - $(Get-Date -UFormat %d-%m-%Y-%H.%M.%S).csv" Write-Host "Copying $($file) > $($newFile)" Copy-Item "$($file)" -Destination "$($newFile)" (Import-CSV $file -Header Email, ItemType | Select-Object "Email", "ItemType" | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1) -replace '"' | Set-Content $file } } function GetSecurePassword ($password) { if ($password) { $securePassword = ConvertTo-SecureString $password -AsPlainText -Force } else { $securePassword = (new-object System.Security.SecureString) } return $securePassword } $WorkFolder = "" $MailGroupAlias = "" $TenantName = "" $ClientAppId = "" $ClientAppCertificate = "" $ClientAppCertificatePassword = "" $Environment = "Global" ImportCloudMModules -WorkFolder $WorkFolder -limitedScope $true $ProcessEmailDriveCsv = @{ WorkFolder = $WorkFolder SecureCertificatePassword = GetSecurePassword($ClientAppCertificatePassword) MailGroupAlias = $MailGroupAlias ClientAppId = $ClientAppId ClientAppCertificate = $ClientAppCertificate Environment = $Environment TenantName = $TenantName } ProcessEmailDriveCsv @ProcessEmailDriveCsv -DisconnectSession #Copy Reports MoveFiles -sourceFolder $WorkFolder -ClientAppId $ClientAppId # SIG # Begin signature block # MIIYJAYJKoZIhvcNAQcCoIIYFTCCGBECAQExDzANBglghkgBZQMEAgEFADB5Bgor # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCBZsGwzK2absL/X # 1olxSSOWvo4YuSuWgONI+yYemqu3taCCFGUwggWiMIIEiqADAgECAhB4AxhCRXCK # Qc9vAbjutKlUMA0GCSqGSIb3DQEBDAUAMEwxIDAeBgNVBAsTF0dsb2JhbFNpZ24g # Um9vdCBDQSAtIFIzMRMwEQYDVQQKEwpHbG9iYWxTaWduMRMwEQYDVQQDEwpHbG9i # YWxTaWduMB4XDTIwMDcyODAwMDAwMFoXDTI5MDMxODAwMDAwMFowUzELMAkGA1UE # BhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExKTAnBgNVBAMTIEdsb2Jh # bFNpZ24gQ29kZSBTaWduaW5nIFJvb3QgUjQ1MIICIjANBgkqhkiG9w0BAQEFAAOC # Ag8AMIICCgKCAgEAti3FMN166KuQPQNysDpLmRZhsuX/pWcdNxzlfuyTg6qE9aND # m5hFirhjV12bAIgEJen4aJJLgthLyUoD86h/ao+KYSe9oUTQ/fU/IsKjT5GNswWy # KIKRXftZiAULlwbCmPgspzMk7lA6QczwoLB7HU3SqFg4lunf+RuRu4sQLNLHQx2i # CXShgK975jMKDFlrjrz0q1qXe3+uVfuE8ID+hEzX4rq9xHWhb71hEHREspgH4nSr # /2jcbCY+6R/l4ASHrTDTDI0DfFW4FnBcJHggJetnZ4iruk40mGtwEd44ytS+ocCc # 4d8eAgHYO+FnQ4S2z/x0ty+Eo7+6CTc9Z2yxRVwZYatBg/WsHet3DUZHc86/vZWV # 7Z0riBD++ljop1fhs8+oWukHJZsSxJ6Acj2T3IyU3ztE5iaA/NLDA/CMDNJF1i7n # j5ie5gTuQm5nf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