PowerShell数组迭代或解析问题? [英] PowerShell array iteration or parsing problem?

查看:62
本文介绍了PowerShell数组迭代或解析问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PowerShell的新手.我正在尝试根据用户的职务来提取用户Active Directory信息,并将其放置在csv文件中.我有两个对应的数组,$ title和$ csvfile.$ title [0]对应于$ csvfile [0],$ title [1]对应于$ csvfile [1],依此类推.这个循环创建了我所有的.csv文件,但没有提取任何用户数据.我可以在脚本中手动输入任何$ title项目,它可以工作,但是当用作数组时,我什么也没得到.

I'm new to PowerShell. I'm trying to pull a users Active Directory info based on their job title and place it in a csv file. I have two corresponding arrays, $title and $csvfile. $title[0] corresponds with $csvfile[0] and $title[1] to $csvfile[1] and so on. This loop creates all my .csv files but doesn't pull any user data. I can manually enter any of the $title items into the script and it works but when used as an array I get nothing.

数组是否添加任何前导或结尾空格,引号,定界符以及任何可能使-and语句无法正常工作的东西?

Does an array add any leading or ending spaces, quotations, delimiters, anything that might keep the -and statement from working?

$title = @(
   'Director of Nursing'
   'Assistant Director of Nursing'
   'Activities Director'
)
   $csvfile = @(
  'DON'
  'ADON'
  'ACTIVITIES'
)

    for($i=0; $i -lt $title.Count; $i++)
{
    #Get-ADUser -Filter { (Title -like "Director of Nursing")  -and  (Company -like "location1") }| Export-Csv c:\temp\$($csvfile[$i]).csv"
    Get-ADUser -filter { (Title -like "$($title[$i])")  -and  (Company -like "Location1") }| Export-Csv "c:\tempPath\$($csvfile[$i]).csv"
}

推荐答案

AD模块重新解析 -Filter 块(并将其转换为适当的LDAP过滤器),最可能的解释是发生这种情况时,它无法解析 $($ title [$ i]).

The AD module re-parses the -Filter block (and turns it into a proper LDAP filter), the likeliest explanation is that it can't resolve $($title[$i]) when that happens.

尝试使用字符串过滤器:

Try with a string filter instead:

Get-ADUser -filter "Title -like '$($title[$i])'  -and  Company -like 'Location1'"| ...

这篇关于PowerShell数组迭代或解析问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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