简体   繁体   English

Powershell更改AD用户属性

[英]powershell change AD user attribute

I need to search through all users in AD, find their attribute "mail" and in this attribute replace @hell.com to @heaven.com 我需要搜索AD中的所有用户,找到他们的属性“ mail”,然后在此属性中将@ hell.com替换为@ heaven.com

I'm stuck at exporting and importing to csv... 我被困在导出和导入到csv中...

Import-Module ActiveDirectory
get-aduser -Filter {samaccountname -like "gmaleev"} -properties * | Select-Object SamAccountName,mail | export-csv -NoTypeInformation d:\1.csv
$impfile = "d:\1.csv"
Import-CSV $impFile
foreach ($user in $users)
{
$sam = $user.samaccountname
$email = $user.mail
write-host "User $sam has mail $mail"
}

It doesn't work, why? 它不起作用,为什么?

The current issue is that you are running a cmdlet to import the data but not saving it. 当前的问题是您正在运行cmdlet来导入数据但未保存。 Your ForEach cmdlet is not processing any data so you should have blank output except that of Import-CSV which should be outputing to console. 您的ForEach cmdlet不会处理任何数据,因此您应该具有空白输出,但应该Import-CSV到控制台的Import-CSV除外。

Also it seems redundant to export the data to import it again. 同样,导出数据以再次导入也显得多余。 I will presume that you have a reason for exporting it. 我假设您有导出它的理由。

Import-Module ActiveDirectory
$impfile = "d:\1.csv"
$results = Get-AdUser -Filter {samaccountname -like "gmaleev"} -properties * | Select-Object SamAccountName,mail 
$results | export-csv -NoTypeInformation d:\1.csv
$results | foreach ($user in $users){
    $sam = $user.samaccountname
    $email = $user.mail
    write-host "User $sam has mail $mail"
}

This should address what you are showing. 这应该可以解决您显示的内容。 It will get the same data and save it to the variable $results . 它将获得相同的数据并将其保存到变量$results Export that data and then piping it to the ForEach to process 导出该数据,然后将其通过管道传输到ForEach进行处理

Your title suggets that you want to change user attributes. 您的标题建议您要更改用户属性。 To do that you would need to use Set-Aduser . 为此,您需要使用Set-Aduser Depending on what your environment is this might not be the best way to manipulate email attributes. 根据您所处的环境,这可能不是操纵电子邮件属性的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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