Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 我可以单独获取广告帐户属性,但不能在foreach循环中获取?_Powershell_Active Directory_Powershell 3.0 - Fatal编程技术网

Powershell 我可以单独获取广告帐户属性,但不能在foreach循环中获取?

Powershell 我可以单独获取广告帐户属性,但不能在foreach循环中获取?,powershell,active-directory,powershell-3.0,Powershell,Active Directory,Powershell 3.0,我正在尝试获取一个CSV,包含一个电子邮件列表,并使用这些电子邮件在AD中查找用户,然后在创建时查找属性,并将其添加到该CSV中的一列 目前,我正在使用以下行进行查找: $created = Get-ADUser -filter "EmailAddress -eq '$email'" -property whenCreated | select -ExpandProperty whenCreated 理想情况下,这应该实例化一个名为$created的变量,然后为

我正在尝试获取一个CSV,包含一个电子邮件列表,并使用这些电子邮件在AD中查找用户,然后在创建时查找
属性,并将其添加到该CSV中的一列

目前,我正在使用以下行进行查找:


    $created = Get-ADUser -filter "EmailAddress -eq '$email'" -property whenCreated | select -ExpandProperty whenCreated 
理想情况下,这应该实例化一个名为
$created
的变量,然后为该变量指定创建日期,不管传递什么电子邮件。这一行单独工作,并与单独的查找一起工作。但是,当我将该行添加到foreach循环中时,
$created
变量为空。它可以很好地构建新的CSV,但是我想要更改的列(
自定义
)仍然没有填充

Import-Module ActiveDirectory
[array]$CSVfile = Import-CSV "C:\Users\blahblahblah\temp\to_upload.csv" -Header "first_name", "last_name", "email", "group", "title", "department", "phone", "address1", "address2", "city", "state", "zip", "country", "custom", "manager_name", "manager_email", "start_date" 
for($i=0; $i -ne $CSVfile.count; $i++) {
    $email = $CSVfile[$i]."email"
    write-host $email
    $created = Get-ADUser -filter "EmailAddress -eq '$email'" -property whenCreated | select -ExpandProperty whenCreated
    Write-Host($CSVfile[$i].Email + ", " + $created) -NoNewline
    Write-Host ""
    $CSVfile[$i].custom= $created
}

$CSVfile | Export-CSV "C:\Users\blahblahblah\temp\to_upload_1.csv"
作为它自己的行,它可以很好地处理变量
$email
,或者其他任何东西,但是在foreach循环中,
$created
变量总是空的。任何其他属性都是如此,例如SamAccountName、Displayname等


有什么想法吗?

我想你想要这样的东西:

Import-Module ActiveDirectory

$headers = "first_name", "last_name", "email", "group", "title", "department", "phone", 
           "address1", "address2", "city", "state", "zip", "country", "custom", 
           "manager_name", "manager_email", "start_date" 

$CSVfile = Import-CSV -Path "C:\Users\blahblahblah\temp\to_upload.csv" -Header $headers
$newData = foreach ($item in $CSVfile) {
    $item.custom = (Get-ADUser -Filter "EmailAddress -eq '$($item.email)'" -Properties Created -ErrorAction SilentlyContinue).Created
    # output the updated item
    $item
}

$newData | Export-CSV "C:\Users\blahblahblah\temp\to_upload_1.csv" -NoTypeInformation

PowerShell将创建时的
whenCreated
映射到名为
Created
的属性,该属性是转换为本地日期时间对象的whenCreated LDAP属性

您可以使用相同的代码单独搜索每封电子邮件吗?这样做了!非常感谢。Theo@bcornw2谢谢你的反馈!