出口公元信息CSV基于用户名输入CSV PowerShell脚本 [英] Export AD Info CSV based on User Name input CSV powershell script

查看:128
本文介绍了出口公元信息CSV基于用户名输入CSV PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过在用户的列表,并取回一个CSV与名称,SAM帐户名,电子邮件

我的输入CSV如下:

 李四
李四
 

下面是目前的code我使用。我不知道是什么问题。用户实际做下指定的DC存在......

 导入模块的ActiveDirectory
函数来获取-ADUsersDetailsCSV
{
    [CmdletBinding()]
    参数
    (
    [参数(强制= $真,位置= 1)]
    [字符串] $ InCSV,

    [参数(强制= $真)
    [字符串] $ OutCSV
    )

如果($ InCSV)
{
    如果(测试的路径-Path $ InCSV)
    {
        $ USERS =进口的CSV $ InCSV -Header名称
        $用户|的Foreach {获取-ADUser便有$ _姓名-Properties * |选择名称,SAM帐户名,邮件} |出口为CSV -Path $ OutCSV

    }#结束测试的文件存在

    其他
    {
        写警告找不到路径$ InCSV',因为它不存在。
    }


} #END确保输入并提供输出文件

} #END功能获取,UsersDetailsCSV
 

下面的错误:

  GET-ADUser便有:找不到该对象的身份:李四下:DC =等等,DC = COM。
在U:\ DATA \ GetADUserInfo PS脚本\ GetADUsersDetailsCSV.psm1:19字符:28
+ $用户|的Foreach {获取-ADUser便有$ _姓名-Properties * |选择名称,SAMAcc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:ObjectNotFound:(名称:ADUser便有)获取-ADUser便有],ADIdentityNotFoundException
+ FullyQualifiedErrorId:找不到该对象的身份:李四下:DC =等等,DC = COM,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 

解决方案

如果您运行获取帮助获取-ADUser便有,你会发现这说明Identity参数:

  -Identity< ADUser便有>
        通过提供以下属性值之一指定Active Directory用户对象。在括号中的标识符是该属性的LDAP显示名称。

      专有名称
        示例:CN = SaraDavis,CN =欧洲,CN =用户​​,DC =公司,DC = CONTOSO,DC = COM
      GUID(的objectGUID)
        示例:599c3d2e-f72d-4d20-8a88-030d99495f20
      安全标识符(的objectSID)
        实施例:S-1-5-21-3165297888-301567370-576410423-1103
      SAM帐户名(sAMAccountName赋)
        示例:saradavis
 

请注意这个名字是不认同它会接受之一。名称是不是在AD中的一个索引属性,因为它不能保证是唯一的。这可能是在你的域名,但AD不知道。要通过任何其他属性获取用户,你需要使用一个过滤器,让你的脚本会是这个样子(我拿着折叠以便于阅读的自由)

  $用户|的foreach {
获取-ADUser便有筛选器名称-eq'$($ _名。)-Properties邮箱|
选择名称,SAM帐户名,邮件} |
出口CSV -Path $ OutCSV
 

另外请注意,名称和samAccountName是其中的获取-ADUser便有将alwasy回归公共属性,所以你必须指定唯一属性是邮件。

我觉得这需要的产生额外的需求照顾,但我没有测试:

  $用户|的foreach {
  $的getUser =
    获取-ADUser便有筛选器名称-eq'$($ _名。)-Properties邮箱|
    选择名称,SAM帐户名,邮件

  如果($的getUser){$的getUser}
   其他{[PSCustomObject] @ {名称= $ _; SAM帐户名='找不到';电子邮件= $空}}
 } |
出口CSV -Path $ OutCSV
 

I need to pass in a list of Users and get back a CSV with Name, SamAccountName, email

My Input CSV is as follows:

"John Doe"
"Jane Doe"

Here's the current code I'm using. I'm not sure what the problem is. The users actually do exist under the "DC" specified...

Import-Module ActiveDirectory
Function Get-ADUsersDetailsCSV
{
    [CmdletBinding()]
    Param
    (
    [Parameter(Mandatory=$True,Position=1)]
    [String]$InCSV,

    [Parameter(Mandatory=$True)]
    [String]$OutCSV
    )

If($InCSV)
{
    If(Test-Path -Path $InCSV)
    {
        $USERS = Import-CSV $InCSV -Header Name
        $USERS|Foreach{Get-ADUser $_.Name -Properties * |Select Name, SAMAccountName, mail}|Export-CSV -Path $OutCSV

    } #End Test that the files exist

    Else
    {
        Write-Warning "Cannot find path '$InCSV' because it does not exist."
    }


} #End Ensure Input and Output files were provided

} #End Function Get-UsersDetailsCSV

Here's the error:

Get-ADUser : Cannot find an object with identity: 'John Doe' under: 'DC=blah,DC=com'.
At U:\data\GetADUserInfo PS Script\GetADUsersDetailsCSV.psm1:19 char:28
+             $USERS|Foreach{Get-ADUser $_.Name -Properties * |Select Name, SAMAcc ...
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Name:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: 'John Doe' under: 'DC=blah,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

解决方案

If you run Get-Help Get-ADUser, you'll find this description for the Identity parameter:

-Identity <ADUser>
        Specifies an Active Directory user object by providing one of the following property values. The identifier in parentheses is the LDAP display name for the attribute.

      Distinguished Name 
        Example:  CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com
      GUID (objectGUID) 
        Example: 599c3d2e-f72d-4d20-8a88-030d99495f20 
      Security Identifier (objectSid) 
        Example: S-1-5-21-3165297888-301567370-576410423-1103
      SAM account name  (sAMAccountName) 
        Example: saradavis

Note that Name is not one of the identities it will accept. Name is not an indexed attribute in AD, because it is not guaranteed to be unique. It probably is in your domain, but AD doesn't know that. To get a user by any other attribute, you need to use a filter, so your script would look something like this ( I took the liberty of folding for readability)

$USERS | Foreach{
Get-ADUser -filter "Name -eq  '$($_.name)'" -Properties mail |
Select Name, SAMAccountName, mail}|
Export-CSV -Path $OutCSV

Also note that Name and SAMAccountName are among the common properties that Get-ADUser will alwasy return, so the only other property you have to specify is Mail.

I think this will take care of the additonal requirements, but I didn't test it:

$USERS | Foreach{
  $getuser = 
    Get-ADUser -filter "Name -eq  '$($_.name)'" -Properties mail |
    Select Name, SAMAccountName, mail

  if ($getuser) {$getuser}
   else {[PSCustomObject]@{Name=$_;SAMAccountName='Not found';mail=$null}}
 } |
Export-CSV -Path $OutCSV

这篇关于出口公元信息CSV基于用户名输入CSV PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆