我正在编写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”作为代理邮件地址
有人可以提出建议,我该如何实现?
最佳答案
您写过的部分:
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
关于powershell - 更改CSV电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682152/