简体   繁体   English

使用 PowerShell 添加文本文件中列出的用户的 smtp 代理地址

[英]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.

相关问题 Powershell - 需要将 AD 组中所有用户的 ProxyAdresses 中的所有 smtp 地址拉入单个列中 - Powershell - Need to pull all smtp addresses from ProxyAdresses for all users in an AD group into a single column 如何搜索多个文本文件的IP地址,并使用PowerShell输出找到文本文件的IP地址? - how to search multiple text files for IP addresses and output the IP addresses found to a text file using powershell? 使用PowerShell从文本文件添加或删除文本 - Add or remove text from text file using PowerShell 使用Powershell获取Office 365 SMTP:电子邮件地址 - Using Powershell to get Office 365 SMTP: email addresses 使用 powershell 从 json 文件获取 ip 地址 - Getting ip addresses from json file using powershell 使用Powershell在jar文件中添加文本文件 - Add a text file in a jar file using powershell Exchange Powershell-批量向所有邮箱用户添加新的SMTP地址 - Exchange Powershell - Bulk add new SMTP address to all mailbox users 如何使用PowerShell将用户从CSV文件添加到Active Directory(AD)和Exchange中? - How do you add users from a CSV file to Active Directory (AD) and Exchange using PowerShell? PowerShell 将文件复制到文本或 csv 文件中列出的多个文件夹的脚本 - PowerShell Script to copy file to multiple folders listed in text or csv file Powershell:从他们的代理地址获取特定域 email 地址 - Powershell: Get specific domain email address from their proxy addresses
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM