我正在编写PowerShell代码以将电子邮件地址导出到csv文件并进行编辑。我写了以下内容:
# Script to get all DLs and email addresses in csv file
Get-ADGroup -Filter 'groupcategory -eq "distribution"' -Properties * |
select Name, mail |
Export-csv "C:\temp\Distribution-Group-Members.csv"
# Following will import the csv, make new column as proxy and save the file
# as update.csv
Import-Csv "C:\temp\Distribution-Group-Members.csv" |
Select-Object "Name", "mail", @{n = "proxy"; e = "mail"} |
Export-Csv "c:\temp\Distribution-Group-Members-Updated.csv" -NoTypeInfo
# Following script can import the csv and set proxy addresses from proxy
# column
Import-Csv "c:\temp\Distribution-Group-Members-Updated.csv" |
Foreach {
Get-ADGroup $_.Name | Set-ADGroup -Add @{
proxyaddresses = ($_.proxy -split ";")
}
}
现在,我想在脚本中再添加2个功能:
因此,假设我的DL名称为“ DL Test”,电子邮件为“ test@abc.com” =>该脚本应将DL的电子邮件地址更新为“ test@xyz.com”,并添加“ smtp:test @ abc”。 com”作为代理邮件地址
有人可以请教,我该如何实现?
答案 0 :(得分:3)
您写过的部分:
select-object "Name", "mail", @{n = "proxy"; e = "mail"}|
proxy
部分称为计算属性。仅使用名称Name
和Mail
的前两个是直接从输入对象中复制的,但是使用@{..}
语法,您可以放置代码来计算新值。
因此,您可以使用它来实现两个所需的更改:
Import-Csv -Path 'C:\temp\Distribution-Group-Members.csv' |
Select-Object -Property Name,
@{Label='Mail'; Expression={$_.Mail -replace 'abc', 'xyz'}},
@{Label='Proxy'; Expression={"SMTP:$($_.Mail -replace 'abc', 'xyz');smtp:$($_.Mail)"}}|
Export-csv 'C:\temp\Distribution-Group-Members.csv' -NoTypeInformation