Hi,
I’ve been creating a script which sets a variable to users found in an OU and then runs a foreach script block against them. Everything works fine, however, there’s now a requirement that input for the foreach block to be fed by csv file rather than OU.
Currently I have the following:
$TargetUsers = Get-ADUser -Filter * -SearchBase "OU=ContosoUsers,OU=Contoso,DC=domain,DC=local" | Sort-Object UserPrincipalName Get-ADUser -Filter * -SearchBase "OU=ContosoUsers,OU=Contoso,DC=domain,DC=local" -Properties * | Select UserPrincipalName | Export-CSV $UsersToMigrate -NoTypeInformation -Force foreach ($User in $TargetUsers) { $Upn = $User.UserPrincipalName Set-MsolUserLicense -UserPrincipalName $Upn -LicenseOptions $O365EntE3 "$($Upn): All Office 365 Enterprise E3 services enabled" | Tee-Object $UserMigrationLog -Append Try { Set-MsolUserLicense -UserPrincipalName $Upn -AddLicenses CompanyName:EMS -ErrorAction Stop "$($Upn): EMS Product enabled" | Tee-Object $UserMigrationLog -Append } Catch { Write-Warning "$($Upn): EMS Licence already assigned" Write-Output "$($Upn): EMS Licence already assigned" | Out-File $UserMigrationLog -Append } If ($EmailMigration) { Do { Start-Sleep -s 15 Write-Warning "Please wait. $($Upn) mailbox is currently being setup" } Until (Get-Mailbox -Identity $Upn) "$($Upn): Mailbox ready for migration" | Tee-Object $UserMigrationLog -Append } }
The export to CSV here is simply exporting the relevant users ready for this process.
I’ve tried a few things but feel like I’m getting in a muddle slightly. I’d prefer not change the script block if possible as took some time to get it working correctly but obviously if there’s no alternative then I’m open to suggestions.
Any ideas?