[英]Disable/Enable AD user account from CSV
How can I enable or disable an AD user account from a csv based on an entry.如何根据条目从 csv 启用或禁用 AD 用户帐户。 If the status for both say Active, only one account gets enabled instead of both.如果两者的状态都显示为 Active,则仅启用一个帐户而不是两个帐户。 Same for the disabled status CSV file:禁用状态 CSV 文件相同:
Samaccountname,Status
john.doe,Active
jane.doe,Disabled
What I have so far:到目前为止我所拥有的:
Import-CSV -Path c:\folder\adaccounts.csv
ForEach ($User in $Users)
{
IF ($User.Status -contains "Disabled")
{
Get-ADUser -Identity $user.samaccountname | Disable-ADAccount
}
elseif ($User.Status -contains "Active")
{
Get-ADUser -Identity $user.samaccountname | Enable-ADAccount
}
At the top of your script you are importing the CSV but it doesn't look like you have assigned it to a variable for your foreach loop if you assign it to the $Users
variable like below, the rest of the script should then go through your CSV as expected.在脚本的顶部,您正在导入 CSV 但如果您将其分配给如下所示的$Users
变量,则看起来您并未将其分配给 foreach 循环的变量,那么脚本的 rest 应该是 Z34D1F91FB2E5A89A 到 Z34D1F91FB2E5A89B1A您的 CSV 符合预期。
$Users = Import-Csv -Path c:\folder\adaccounts.csv
-Contains
is an operator to test if something can be found in an array of things, not for testing if a string is equal or not to another string. -Contains
是一个运算符,用于测试是否可以在数组中找到某些内容,而不是用于测试字符串是否等于另一个字符串。
I would revise your code like this:我会像这样修改你的代码:
Import-CSV -Path 'c:\folder\adaccounts.csv' | ForEach-Object {
# test if a user with that SamAccountName can be found
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.Samaccountname)'" -ErrorAction SilentlyContinue
if ($user) {
# set Enabled if Status is not 'Disabled'
$user | Set-ADUser -Enabled ($_.Status -ne 'Disabled')
}
else {
Write-Warning "User $($_.Samaccountname) does not exist"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.