[英]Add smtp Proxy Addresses from users listed in a text file using PowerShell
I am trying to create a simple script to add Proxy Addresses to the AD field using PowerShell.我正在尝试创建一个简单的脚本,使用 PowerShell 将代理地址添加到 AD 字段。
I was able to get it working using this, but now I am at a roadblock on how I can do this importing the usernames from a text file.我能够使用它来让它工作,但现在我在如何从文本文件导入用户名方面遇到了障碍。
$Username = Read-Host -Prompt "Enter the username'
Set-AdUser $Username -add @{ProxyAddresses = "smtp:$Username@example.com,smtp:$Username@marketing.example.com" -split ","}
What I want to do now is instead of prompting for a username to be entered I just want to have a text file with username like this.我现在想要做的不是提示输入用户名,而是我只想拥有一个包含这样用户名的文本文件。
Text File Of Usernames: These will all be on a separate line.用户名文本文件:这些都在单独的一行中。 I am not sure how to format that way on here.我不确定如何在此处格式化。
jallen
sdiggs
gdavis
mhyde
twhite
I am confused how to go forward with this.我很困惑如何转发 go。 To my understanding I want to use Get-Content to create the username array and then for each line in the text file add the proxy addresses.据我了解,我想使用 Get-Content 创建用户名数组,然后为文本文件中的每一行添加代理地址。
$Username = Read-Host -Prompt "Enter the username'
Set-AdUser $Username -add @{ProxyAddresses = "smtp:$Username@example.com,smtp:$Username@marketing.example.com" -split ","}
I want to remove the need for user input and import the username variables from a text file.我想消除对用户输入的需要并从文本文件中导入用户名变量。
Assuming you have the txt file with each user in a new line as shown in your question, you're right, you can use Get-Content
to read your file then you need to loop over each line:假设你有一个 txt 文件,每个用户都在一个新行中,如你的问题所示,你是对的,你可以使用Get-Content
来读取你的文件,然后你需要遍历每一行:
(Get-Content path\to\yourfile.txt).Trim() | ForEach-Object {
try {
Set-AdUser $_ -Add @{ ProxyAddresses = "smtp:$_@example.com,smtp:$_@marketing.example.com".Split(",") }
}
catch {
# can do error handling here
Write-Error $_
}
}
The use of Trim()
in this example is so it removes any excess of white space from the beginning and end of all lines.在此示例中使用Trim()
是为了从所有行的开头和结尾删除任何多余的空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.