【问题标题】:Trying to disable users in AD who are not in a CSV尝试禁用 AD 中不在 CSV 中的用户
【发布时间】:2014-11-05 14:22:19
【问题描述】:

我一直试图在论坛中找到与此类似的东西,但正是这种逻辑将我束缚住了 - 将它们放在一起。

我有一个 AD,并且我有一个应该在特定 OU 中的用户 CSV。我想将 OU 中的用户与 CSV 进行比较,而不是 CSV 中的用户,我想禁用它们并将它们移动到不同的 OU。

我是 Powershell 的新手,在这方面遇到了一些困难。让我印象深刻的是比较和 IF-Then 逻辑……我只是无法正确地使用语法。我已经尝试了一些选择......这就是我现在所拥有的

Import-Module ActiveDirectory
$path     = "f:\aDMGMT\"
$logpath = "f:\admgmt\logs\diable_ad_users.log"
$userfile  = $path + "\files\ad_currentemployees.csv"
$location = "OU=Faculty,OU=People,DC=mydomain,DC=com"
$disabledou = "OU=disabledemployees,OU=Disabled,DC=mydomain,DC=com"
$AD_users =  Get-ADUser -Filter * -SearchBase "OU=Faculty,OU=People,DC=mydomain,DC=com" | select -ExpandProperty SamAccountName

$sams = $userfile | Select-Object -ExpandProperty NameUnique #the
Compare-Object $AD_users $sams | Out-File $logpath

但是可用的标签是 includeequal 和 excludedifferent...但不是 includedifferent...我将如何只为一侧做呢?

救命!

【问题讨论】:

    标签: powershell csv active-directory


    【解决方案1】:

    您可以做的是使用SideIndicator 将结果传递到where 子句中进行过滤。

    Compare-Object $AD_users $sams | 
        Where-Object{$_.SideIndicator -eq "<="} | 
        Select-Object -expandproperty inputobject
    

    使用您需要的方向,"&lt;=""=&gt;",然后您将通过管道进入Select-Object 以恢复您正在过滤的输入对象。如果您的对象是多维的,那么最后一部分会更重要。

    我将使用真实数据进行尝试,因为这只是经过简短测试但应该可以工作。

    【讨论】:

    • 我知道我的日志文件中的那些箭头会有所帮助,但我是通过 ImportCSV 来处理它们,而不是使用 Where-Object - 我觉得很傻,我没有遇到这个。我稍后会尝试一下,然后告诉你它是怎么回事!
    • 好的,我试过这个,但在选择 AD 方面的东西时我没有骰子。在您有 inputobject 占位符的地方替换“SAMAccountName”时,我收到一条错误消息。有什么见解吗?
    • @JenDavenport 不要使用替代品。 inputobject 是compare-object 返回的实际属性
    【解决方案2】:

    我重新思考了我的逻辑并想出了这个。效果很好。

    Import-Module ActiveDirectory -ErrorAction Stop
    $path       = "f:\aDMGMT\"
    $date       = Get-Date
    $logdate    = Get-Date -UFormat "%y%m%d"
    $log        = $path+"\logs\diable_ad_users_"+$logdate+".log"
    $userfile   = $path + "\files\ad_currentemployees.csv"
    $location   = "OU=employees,OU=People,DC=myorg,DC=com"
    $disabledou = "OU=disabledemployees,OU=Disabled,DC=myorg,DC=com"
    $AD_users   =  Get-ADUser -Filter * -SearchBase "OU=employees,OU=People,DC=myorg,DC=com" | select -ExpandProperty SamAccountName
    $sams       = Import-csv $userfile | select nameunique
    
    ForEach ($user in $AD_users)
    {
    $exists = $sams.nameunique -contains $user # clean output for array w/ header vs. array without header demands .namunique
    If(!$exists)
    {
        Get-ADUser -Identity $user | Move-ADObject -targetpath $disabledou
        Disable-ADAccount -Identity $user
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多