Public/Trash.ps1
function Reset-DomainNamesForUsers { param( $Users, $MatchDomains, $FieldTypes ) <# foreach ($User in $DataFinland) { $User.UserPrincipalName = $($User.UserPrincipalName).ToLower().Replace('@test.com', '@newdomain.com') $User.License = $($User.License).ToLower().Replace('test:', 'newdomain:') $User.ProxyAddress = $(($User.ProxyAddress).ToLower()).Replace('@test.com', '@newdomain.com').Replace('@test.onmicrosoft.com', '@newdomain.onmicrosoft.com') } #> foreach ($User in $Users) { foreach ($Match in $MatchDomains.Keys) { $Key = $Match $Value = $MatchDomains.$Match foreach ($Field in $FieldTypes) { $User.$Field = $($User.$Field).ToLower().Replace($Key, $Value) } } } return $Users } function Remove-UserExistingAccounts { param( $Users, $ExistingUsers ) $UsersExisting = @() $UsersNonExistant = @() foreach ($User in $Users) { if ($ExistingUsers.UserPrincipalName -contains $User.UserPrincipalName) { $UsersExisting += $User } else { $UsersNonExistant += $User } } return $UsersNonExistant } function Set-AddPasswords { param( $Users ) foreach ($User in $Users) { $Password = Get-RandomPassword $NickName = ($User.UserPrincipalName).Split('@') $User | Add-Member -MemberType NoteProperty -Name 'Password' -Value $Password -Force $User | Add-Member -MemberType NoteProperty -Name 'MailNickName' -Value $NickName[0] -Force } return $Users } function New-UserAdd { [CmdletBinding()] param( $Users ) $Data = @{} $Success = @() $Failed = @() foreach ($User in $Users) { #New-MsolUser -UserPrincipalName $User.UserPrincipalName -FirstName $User.FirstName -LastName $User.LastName -DisplayName $User.DisplayName -UsageLocation $User.CountryCode -Country $User.Country -City $User.City -WhatIf $PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile $PasswordProfile.Password = $User.Password $PasswordProfile.EnforceChangePasswordPolicy = $false $PasswordProfile.ForceChangePasswordNextLogin = $false try { Write-Color "New-AzureADUser - Processing new user ", $User.DisplayName -Color White, Yellow $Output += New-AzureADUser -UserPrincipalName $User.UserPrincipalName ` -GivenName $User.FirstName ` -Surname $User.LastName ` -DisplayName $User.DisplayName ` -UsageLocation $User.CountryCode ` -Country $User.Country ` -City $User.City ` -PasswordProfile $PasswordProfile ` -AccountEnabled $true ` -MailNickName $User.MailNickName ` -ErrorAction Stop $Success += $User } catch { $Failed += $User $ErrorMessage = $_.Exception.Message -replace "`n", " " -replace "`r", " " Write-Warning "New-AzureADUser - Failed with error message: $ErrorMessage" } } $Data.Failed = $Failed $Data.Success = $Success return $Data } function Set-SpecUser { param( $User, $UsersAzure ) $UserAzure = $UsersAzure | where { $_.UserPrincipalName -eq $User.UserPrincipalName } if ($UserAzure) { Write-Color "Set-SpecUser - Processing user ", $User.DisplayName, ' - ObjectID: ', $($UserAzure.ObjectID), ' user password ', $User.Password -Color White, Yellow $Password = $User.Password | ConvertTo-SecureString -AsPlainText -Force Set-AzureADUserPassword -ObjectId $UserAzure.ObjectID -Password $Password } else { Write-Color "Set-SpecUser - Skipping user ", $User.DisplayName, ' - ObjectID: ', $($UserAzure.ObjectID), ' user password ', $User.Password -Color White, Yellow } } |