Import CSV file in foreach block

Welcome Forums General PowerShell Q&A Import CSV file in foreach block

This topic contains 2 replies, has 1 voice, and was last updated by

1 year, 7 months ago.

  • Author
  • #75817

    Points: 0
    Rank: Member


    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
                    Set-MsolUserLicense -UserPrincipalName $Upn -AddLicenses CompanyName:EMS -ErrorAction Stop            
                    "$($Upn): EMS Product enabled" | Tee-Object $UserMigrationLog -Append
                    Write-Warning "$($Upn): EMS Licence already assigned"
                    Write-Output "$($Upn): EMS Licence already assigned" | Out-File $UserMigrationLog -Append
                    If ($EmailMigration) {
                            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?

  • #75833

    Points: 0
    Rank: Member

    OK, so I'm thinking along the lines of setting the $TargetUsers variable like this instead:

    Get-ADUser -Filter * -SearchBase 'OU=ContosoUsers,OU=Contoso,OU=Organisation,DC=domain,DC=local' -Properties * | Select UserPrincipalName | Sort UserPrincipalName | Export-CSV $UsersToMigrate -NoTypeInformation -Force
    $TargetUsers = Import-Csv "C:\Temp\O365Migration\O365ExchangeMigration#4.csv" | ForEach-Object {Get-ADUser -Filter "UserPrincipalName -like '*$($_.UserPrincipalName)*'" -Properties UserPrincipalName} | Select UserPrincipalName

    The only header is UserPrincipalName and this is included when reading the variable $TargetUsers so I looked at then exporting it without the header with the following:

    (Get-ADUser -Filter * -SearchBase 'OU=ContosoUsers,OU=Contoso,OU=Organisation,DC=domain,DC=local' -Properties * | Select UserPrincipalName | Sort UserPrincipalName | ConvertTo-Csv -NoTypeInformation) | Select-Object -Skip 1 | Set-Content -Path $UsersToMigrate

    ...but this then can't perform the Get-ADUser cmdlet as -like can't compare to to a header.

    Am I misunderstanding something here?

    • #75893

      Points: 0
      Rank: Member

      Ok, turns out I had it licked above. My second post to set the variable by importing the csv and then piping through to Get-ADUser appears to have worked. I was thinking the header returned when reading the variable $TargetUsers was a row that would be processed but of course this is just what Get-ADuser cmdlet is returning in the same way it would from my original method to obtain users based on the searchbase OU. Obviously only objects returned would be processed and testing has shown this.


The topic ‘Import CSV file in foreach block’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort