【问题标题】:Import a CSV Find an AD account by EmployeeID attribute and export to a CSV导入 CSV 按 EmployeeID 属性查找 AD 帐户并导出到 CSV
【发布时间】:2019-01-18 06:18:36
【问题描述】:

早上好,

我需要一个 Powershell 脚本来导入 csv,通过 AD 中的 EmployeeID 属性查找用户,并导出 csv 以验证帐户已被禁用。我只需要在导出中显示名称、员工 ID 和启用/禁用。

我对 Powershell 还是很陌生,我通常可以剪切/粘贴和操作现有脚本来做我需要的事情,但我似乎无法让它工作,现在我的代码在老鼠接触到它后看起来就像瑞士奶酪.这是我所拥有的,但我不确定我是否接近所需的。

#Script Starts
$Users = Import-Csv "\folder\Test.csv"

Import-Module ActiveDirectory
$path = Split-Path -parent ".\folder\*.*"

#Create log date
$logdate = Get-Date -Format yyyy-MM-dd-THH.mm.ss
$logfile = $path + "\logs\$logdate.logfile.txt"

ForEach ($User In $Users)
{
   # Retrieve values from the csv by header name.
   $FirstName = $User.FirstName
   $LastName = $User.LastName
   $ID = $User.Employee 

   #Search AD for Attributes
   $UserSN = (Get-ADUser -LDAPFilter "(employeeID=$ID)").sAMAccountName
   $UserDN = (Get-ADUser -Identity $UserSN -Properties DisplayName).DisplayName
   $Enabled = (Get-ADUser -Identity $UserSN -Properties Enabled).Enabled
   Export-Csv -Path $logdate.ADEnabled-Disabled.csv -Append
} 
#Finish

【问题讨论】:

    标签: powershell csv attributes import-csv export-csv


    【解决方案1】:

    鉴于所有这些 cmets 以及已经给出的不完整答案,我担心 OP 会更加困惑......

    为了让OP更好地理解,我尽可能地坚持他的原始代码。

    第一:

    我假设通过查看代码,输入的 csv 文件看起来与此类似

    "FirstName","LastName","Employee"
    "Elvis","Presley","12345"
    "Axl","Rose","987654"
    "Janis","Joplin","654283"
    

    我在这里看到的主要问题是代码正在使用 EmployeeID 属性(字符串)搜索 AD 用户。通过将过滤器用作eq,在 AD 中找到的字符串必须与 csv 中的字符串完全相同(尽管不区分大小写)。如果不是这种情况,代码将无法通过使用 -eq 找到用户,但也许通过比较 CSV 和 AD 中的内容,可以使用不同的运算符,例如 -like。这是由 OP 弄清楚的。

    接下来是代码:

    #Script Starts
    $Users = Import-Csv "\folder\Test.csv"
    
    Import-Module ActiveDirectory
    $path = Split-Path -parent ".\folder\*.*"
    
    # Create log date
    $logdate = Get-Date -Format yyyy-MM-dd-THH.mm.ss
    $logfile = $path + "\logs\$logdate.logfile.txt"
    # while you're at it, create the full path and filename for the output csv
    $outFile = Join-Path $path $($logdate + ".ADEnabled-Disabled.csv")
    
    # create an array for the output file
    # You CAN do without by letting PowerShell do the aggregating of objects for you
    # but in this case I think it will make things more readable to collect ourselves.
    $result = @()
    ForEach ($User In $Users) {
        # Find the user by the 'Employee' field in the csv and return an object with the required properties
        # Use -LDAPFilter which requires the LDAP syntax: "(employeeID=$($User.Employee))"
        # Or use -Filter where you use the Powershell syntax
        $adUser = Get-ADUser -Filter "EmployeeID -eq $($User.Employee)" -Properties DisplayName, Enabled, EmployeeID |
                  Select-Object DisplayName, Enabled, EmployeeID
    
        if ($adUser) {
            # if the above statement did find the user, we add this to the result array
            $result += $adUser
        }
        else {
            $msg = "User '{0} {1}' not found. Check the Employee id {2}." -f $user.FirstName, $user.LastName, $user.Employee
            Write-Warning $msg
            Add-content $logfile -Value $msg
        }
    } 
    
    # now write the output csv as a new file. Because it has a complete date and time in the name
    # the chances of overwriting an existing file are very slim indeed, but we'll use -Force anyway.
    $result | Export-Csv -Path $outFile -NoTypeInformation -Force
    #Finish
    

    希望对你有帮助

    【讨论】:

    • 谢谢你,这段代码很棒,但请耐心等待我的理解。 $result = @() 启动数组,该数组将数据保存在内存中,并且是进行 csv 比较/搜索的首选/有效方式。 ForEach ($User in $Users) 是保存 csv 中每一行 ($User) 的循环 ($Users) -Filter 使用 csv 中的 Employee 列来搜索 EmployeeID 属性并且只提取 3 个属性,但它是 Select-Object 语句将这 3 个属性放入 $adUser 变量中。如果找到,则将属性放入数组 Else 警告消息中。完成后创建 CSV。
    • 你是正确的!
    • 太好了,谢谢。我会弄清楚这个 PowerShell 的事情(希望迟早):D 感谢大家对此的帮助。
    【解决方案2】:

    为了Export-Csv,您需要某种类型的输入。目前您没有导出任何内容。

    理想情况下,您希望创建一个包含所有项目的数组,然后导出该数组。

    $x = @()
    ForEach ($User In $Users)
    {
       $x += [pscustomobject]@{
         FirstName = $User.FirstName
         LastName = $User.LastName
         #etc...
       }
    } 
    
    $x | Export-Csv -Path "$logdate.ADEnabled-Disabled.csv"
    

    你有一些小问题,但我认为这会让你朝着实际结果努力。

    【讨论】:

    • 感谢您的回复,现在它告诉我 Get-ADUser :无法识别搜索过滤器 $UserSN = ... 显然我的 $ID 变量为空,我将其写入日志文件并它是空白的......我做错了什么?
    • 我修复了空白 $ID 变量,但仍然出现“无法识别搜索过滤器”错误。任何帮助表示赞赏。
    • -过滤器有不同的语法。 -Filter "employeeID -eq `"$ID`"" 或 -Filter "employeeID -like `"*$ID*`"" 是正确的 iirc。
    • 好点,但$logdate.ADEnabled-Disabled.csv 没有封闭"..." 中断 Export-Csv 调用。此外,在$x 中简单地收集整个foreach 语句的输出(而不是增量地“添加”到数组中,这会在每次迭代中重新创建它)效率更高,类似于以下示例:$x = foreach ($num in 1..3) { $num * 2 }。为避免将整个集合保存在内存中,请使用Import-Csv | ForEach-Object ... | Export-Csv。
    • 检查我的编辑和@mklement0 的评论。 Export-csv 可能不起作用,因为您缺少将 $var.x 定义为 $var+string,而不是 $var+property 的引号。
    猜你喜欢
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2015-08-11
    • 2022-10-09
    相关资源
    最近更新 更多