我有一个源.csv文件,其中包含columnA emailaddress
和columnB Is this mailbox still in use?
。
我需要读取文件,根据给定的SamAccountName
属性找到ADUser的emailaddress
,然后在columnB = NO
的地方删除任何AD帐户。如果B列= YES
,请更新属性。
第二部分,如果Yes
很好用,但是我无法使第一部分正常工作,因为我不确定如何获取然后将SamAccountName
传递给Remove-ADUser
命令。任何建议都会很棒。
$Users = Import-Csv $SurveyResults
foreach ($user in $users) {
if ($($user."Is this mailbox still in use?") -eq "NO") {
Get-ADUser -Filter "emailaddress -eq '$($user.emailaddress)'" -Property SamAccountName |
Select-Object -Property emailaddres, samaccountname, wwWHomePage |
Remove-ADUser -Identity $_.SamAccountName -Confirm:$False
}
else {
Get-ADUser -Filter "emailaddress -eq '$($user.emailaddress)'" -Properties * |
Set-AdUser -replace @{wWWHomePage = "$($_.wWWHomePage) ACTIVE $(get-date -f yyyy-MM)"}
}
}
答案 0 :(得分:1)
Remove-ADUser
可以通过管道获取ADUser对象。如果您确定只能从过滤器中获得一个ADUser对象,或者确定要删除所有具有匹配电子邮件地址的用户,则可以将Get-ADUser
的结果传递给Remove-ADUser
:
$Users = Import-Csv $SurveyResults
foreach ($user in $users) {
if ($($user."Is this mailbox still in use?") -eq "NO") {
Get-ADUser -Filter "emailaddress -eq '$($user.emailaddress)'" | Remove-ADUser -Confirm:$False
} else {
Get-ADUser -Filter "emailaddress -eq '$($user.emailaddress)'" -Properties * |
Set-AdUser -replace @{wWWHomePage="$($_.wWWHomePage) ACTIVE $(get-date -f yyyy-MM)"}
}
}
答案 1 :(得分:0)
乍一看,我会使用以下内容:
添加了编辑*
$Users = Import-Csv $SurveyResults
foreach ($user in $users)
{
$getADUser = Get-ADUser -Filter "emailaddress -eq '$($user.emailaddress)'" -Properties *
if ($($user."Is this mailbox still in use?") -eq "NO")
{
$getADUser | Remove-ADUser -Confirm:$false
}
else
{
$getADUser | Set-AdUser -replace @{wWWHomePage="$($_.wWWHomePage) ACTIVE $(get-date -f yyyy-MM)"}
}
}