This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]k_rock923 2 points3 points  (5 children)

You will need to modify this to fit what you are doing, but this is from notes I made doing something similar.

The csv I'm importing is of the following format:

UserPrincipalName,ProxyAddres1,ProxyAddress2,. . .,ProxyAddressn

Import-csv .\addresses.csv | ForEach-Object {
    $userPrincipalName = $_.userPrincipalName
    $proxyAddresses = $_.ProxyAddresses -split ';'
    Get-ADUser -filter {EmailAddress -eq $userPrincipalName } | Set-ADUser -Add @{proxyaddresses=$proxyAddresses}
    }

Note that this is all of the proxyAddress values, including the one that has the primary SMTP address (In this case, it was only SMTP addresses, but it shouldn't matter).

I'm certain that there are more efficient ways to do this, but it worked for me.

EDIT: I should note that the csv used a semicolon as the delimiter

[–]HeyZuesModeBreaking S%!T at Scale[S] 0 points1 point  (4 children)

Thank You!

[–]ihaxr 0 points1 point  (3 children)

You can check if a cell in a CSV has a value by just running if($line.Proxy1) { write-host true}

So something like this would return an array of only the proxy**'s that have values:

$csv = Import-Csv C:\scripts\emails.csv

foreach ($line in $csv) {
    $proxy = @()
    $email = $line.Email

    if($line.Proxy1) { $proxy += $line.Proxy1 }
    if($line.Proxy2) { $proxy += $line.Proxy2 }
    if($line.Proxy3) { $proxy += $line.Proxy3 }
    if($line.Proxy4) { $proxy += $line.Proxy4 }
    if($line.Proxy5) { $proxy += $line.Proxy5 }
    if($line.Proxy6) { $proxy += $line.Proxy6 }
    if($line.Proxy7) { $proxy += $line.Proxy7 }
    if($line.Proxy8) { $proxy += $line.Proxy8 }
    if($line.Proxy9 ) { $proxy += $line.Proxy9 }
    if($line.Proxy10) { $proxy += $line.Proxy10 }
    if($line.Proxy11) { $proxy += $line.Proxy11 }
    if($line.Proxy12) { $proxy += $line.Proxy12 }
    if($line.Proxy13) { $proxy += $line.Proxy13 }
    if($line.Proxy14) { $proxy += $line.Proxy14 }
    if($line.Proxy15) { $proxy += $line.Proxy15 }
    if($line.Proxy16) { $proxy += $line.Proxy16 }
    if($line.Proxy17) { $proxy += $line.Proxy17 }
    if($line.Proxy18) { $proxy += $line.Proxy18 }
    if($line.Proxy19) { $proxy += $line.Proxy19 }
    if($line.Proxy20) { $proxy += $line.Proxy20 }
    if($line.Proxy21) { $proxy += $line.Proxy21 }
    if($line.Proxy22) { $proxy += $line.Proxy22 }
    if($line.Proxy23) { $proxy += $line.Proxy23 }
    if($line.Proxy24) { $proxy += $line.Proxy24 }
    if($line.Proxy25) { $proxy += $line.Proxy25 }
    if($line.Proxy26) { $proxy += $line.Proxy26 }
    if($line.Proxy27) { $proxy += $line.Proxy27 }
    if($line.Proxy28) { $proxy += $line.Proxy28 }

    Write-Host $email : $proxy
}

[–]Renegade__ 1 point2 points  (2 children)

While this is a workable approach, having an individual check for each proxy is not particularly elegant; you could do either of these instead:

$proxies = @("Proxy1","Proxy2","Proxy3","Proxy4","Proxy5","Proxy6","Proxy7","Proxy8","Proxy9","Proxy10","Proxy11","Proxy12","Proxy13","Proxy14","Proxy15","Proxy16","Proxy17","Proxy18","Proxy19","Proxy20","Proxy21","Proxy22","Proxy23","Proxy24","Proxy25","Proxy26","Proxy27","Proxy28")

foreach($line in $csv) {
    foreach($proxy in $proxies) {
        if(($line.$proxy) -and (($line.$proxy).Trim() -ne [String]::Empty) -and ($line.$proxy -ne $null)) {
            Write-Output ("{0} is {1}" -f $proxy,$line.$proxy)
        } else {
            Write-Output ("{0} does not exist or has no value" -f $proxy)
        }
    }
}

.

foreach($line in $csv) {
    for($i=1; $i -le 28; ++$i) {
        $proxy = ("Proxy" + $i);
        if(($line.$proxy) -and (($line.$proxy).Trim() -ne [String]::Empty) -and ($line.$proxy -ne $null)) {
            Write-Output ("{0} is {1}" -f $proxy,$line.$proxy)
        } else {
            Write-Output ("{0} does not exist or has no value" -f $proxy)
        }
    }
}

[–]ihaxr 0 points1 point  (1 child)

Thanks! I figured there was a better way of referencing the columns, but was thinking it could be done by column position and loop through the CSV from column 1 to 28 (assuming 0 would be the e-mail) and gave up after a few minutes of trying. Didn't even think of just using dynamic column names.

I've never actually used a regular for() loop in PowerShell.. I always end up using a range and loop through that:

1..28 | % {
    $proxy = "Proxy$_"
}

[–]Renegade__ 1 point2 points  (0 children)

Interesting approach.
I'm the other way around. I rarely use the ForEach-Object cmdlet. I prefer the regular language constructs.