简体   繁体   中英

Proxyaddresses added to the multivalued attribute not appearing on seperate lines

I am using the following script to create a spreadsheet of users and proxyaddress values (matching a string) as a record prior to removing them, in the case of an emergency I want to be able to put the values back.

The script works well if there is only a single entry per user, the issue is seen when a user has more than one entry. The problem is that when I attempt to revert back to the original values, when viewed through an attribute editor the proxyaddresses are appearing all on one line instead of a separate line for each proxy address. I am not sure if the fault is on the collection script, or the script I am using to set the values. To collect the values I am running the following:

$entry = "*test.com"
$users = get-content "Testusers.txt"
$date = get-date
$mydata = @()
$domain = [system.environment]::UserDomainName 
$attribute = "Proxyaddresses"
$report = "{0}_{1}_{2:HHmm_dd-MM-yyyy}.txt" -f $attribute,$domain,$date
$px = $users | Get-ADUser -Properties proxyaddresses -server $domain

foreach ($user in $px){
if ($user.proxyaddresses -like $entry){
    $name = $user.samaccountname
    $proxyaddresses = $user.proxyaddresses -like $entry
    $mydata += New-Object PSObject -Property @{
        Samaccountname = $name
        Proxyaddresses = $proxyaddresses
    }
  }
}
$mydata | select samaccountname,@{ l = "Proxyaddresses"; e = {$_.Proxyaddresses } }| export-csv "$PWD\$report" -NoTypeInformation -Append

To return the values back to the original state I am running the following:

$csv = import-csv "Proxyaddresses_Domain_1201_05-04-2017.csv"
$domain = [system.environment]::UserDomainName 
$attribute = "Proxyaddresses"
foreach ($line in $csv){
$user = $line.samaccountname
$proxyaddresses = $line.proxyaddresses
Get-aduser $User -server $domain -Properties $attribute | Set-ADUser -add 
@{$attribute = $proxyaddresses} -server $domain
}

I have tried various things such as in the collection script

$proxyaddresses = $line.proxyaddresses -join ","

I have also tried the same tactic in the script used to set the values but instead of creating a new line it removes a space between the entries on the same line when viewed through an AD attribute editor.

I have also tried to change

$proxyaddresses = $user.proxyaddresses -like $entry 

to the following however this did not solve the problem

$proxyaddresses = ([Microsoft.ActiveDirectory.Management.ADPropertyValueCollection]$user.proxyaddresses -like $entry

HELP!!!

When you are exporting the data, change your select statement from this

select samaccountname,@{ l = "Proxyaddresses"; e = {$_.Proxyaddresses } }

to this

select samaccountname,@{Name="Proxyaddresses";Expression={$_.proxyaddresses -join "*"}}

Which will convert the ADPropertyValueCollection to a * separated string. If you are exporting to a CSV, you don't want to use a comma as the join separator as it will mess up the CSV. I've used a * in the example, as exchange uses colons and semi colons already.

Then when you re-import it, just make sure to convert the * seperated string back into a string array, and use -replace instead of -add

Set-ADUser -replace @{$attribute = $proxyaddresses.Split("*")} -server $domain

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM