【问题标题】:powershell- script with Activ Directory带有 Active Directory 的 powershell 脚本
【发布时间】:2015-02-22 16:20:23
【问题描述】:

我在 powershell 中创建了一个脚本,它为我提供了来自 activ 目录的 csv 文件。 我有 3 个问题。

  1. 我想删除所有空的用户。例如:user1, 班级,房间..这些用户不是电子邮件地址或姓氏。我 想要创建一个过滤器来删除这些行。

  2. 我要添加几个空列

  3. 从长字符串中分离出两个单词。

这是我写的那一行:

Get-ADUser -Filter * -Properties * | Sort-Object -Property SamAccountName  |Select GivenName , Surname, SamAccountName,OfficePhone, 
MobilePhone,EmailAddress, office, Department , Manager   |Export-csv c:\Test2.csv

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    我不知道您想对第三个问题做什么,因为您没有提供任何详细信息或之前和之后的示例。至于 1. 和 2.,试试这个:

    #Gets only required properties to increase performance
    Get-ADUser -Filter * -Properties OfficePhone, MobilePhone, EmailAddress, Office, Department, Manager |
    #Skip objects that's missing both surname and emailaddress
    Where-Object { $_.EmailAddress -or $_.Surname } |
    Sort-Object -Property SamAccountName |
    #Added EmtpyColumn1 and EmptyColumn2 as empty strings, and extracts manager from manager DN-value.
    Select GivenName , Surname, SamAccountName, OfficePhone, MobilePhone, EmailAddress, Office, Department , @{n="Manager";e={$_.Manager -replace 'CN=(.*?),.*', '$1'}}, @{n="EmptyColumn1";e={""}}, @{n="EmptyColumn1";e={""}} #|
    #Added -NoTypeInformation to make it a valid CSV-file. Removes the #TYPE .... that's normally line 1 in the csv-file
    Export-csv c:\Test2.csv -NoTypeInformation
    

    【讨论】:

    • 感谢您的回复。 1)它运作良好:) 2)添加我一个新列,但不是空的:“Microsoft.ActiveDirectory.Management.ADPropertyValueCollection”我如何添加一个新的空列? 3) 我想像这样饶恕我组织中的经理:CN=boaz avi,OU=Department name,DC=company name,DC=org,DC=il 我只希望:“boaz avi”在我的行中,没有其余的..
    • 是的,看看那个。通常你会得到一个空值列,但我猜 PowerShell 中的 ADobjects 在调用时会动态创建属性,所以你会得到一个属性值属性。尝试更新答案。
    猜你喜欢
    • 2023-01-19
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多