从Office属性powershell的CSV获取组织单位

从Office属性powershell的CSV获取组织单位,powershell,Powershell,我不知道为什么我不能让这个工作,但我有一个.csv的办公室。我想从Office属性powershell的CSV中获取组织单位。但是,我想从中排除特定的OU。OU:OU=CompanyIT,OU=COMPANY,DC=contoso,DC=com e、 g OU结构: COMPANY ---SALES ---IT ---SOFTWARE ---CompanyIT 输入文件: Office IT Office 12 Sales Office 我的剧本:

我不知道为什么我不能让这个工作,但我有一个.csv的办公室。我想从Office属性powershell的CSV中获取组织单位。但是,我想从中排除特定的OU。OU:OU=CompanyIT,OU=COMPANY,DC=contoso,DC=com

e、 g OU结构:

COMPANY
   ---SALES
   ---IT
        ---SOFTWARE
   ---CompanyIT
输入文件:

Office
IT Office 12
Sales Office
我的剧本:

    $userIDs = Import-CSV $CSV
    foreach($userID in $userIDs)
{ 
    Get-ADUser -Filter "Office -eq $($userID.Office)" -Properties DistinguishedName 
}
我的期望输出:

User,Office,DN
John,IT Office 12,OU=IT,OU=COMPANY,DC=contoso,DC=com
Maria,Sales Office,OU=SALES,OU=COMPANY,DC=contoso,DC=com

如果我理解正确的话,
userid
变量会让人困惑。如果我把变量名改为office,会更清楚

$Offices = Import-CSV $CSV
foreach($office in $offices){ 
     Get-ADUser -Filter "Office -eq '$($office.Office)'" -Properties office |
     where {$_.DistinguishedName -notlike "OU=CompanyIT,OU=COMPANY,DC=contoso,DC=com"}| 
     select samaccountname,office, @{l="OU";e={($_.distinguishedname -split "," | select -skip 1) -join ","}}    
}

请显示导入CSV文件的前三行(已消毒)。我已更新我的问题。顺便说一句,我想从中排除特定的OU。OU=CompanyIT,OU=COMPANY,DC=contoso,DC=comIt仍然不清楚。。您的输入文件不显示用户(samaccountnames)。代码是否应该从组成员处获取这些信息?所需的DN是用户的DN还是组的DN?然后我认为@Itchydon解决了这个问题。如果是这样,请不要忘记通过单击复选标记图标来接受它。谢谢,我想这样使用过滤。我说得对吗?{(emailaddress-like“@contoso.com”)-和(Enabled-eq$true)-和(sAMAccountName-like“STUFF”)-和(“Office-eq$($Office.Office))}用于过滤的语法如下(不要使用花括号):“emailaddress-like'*@contoso.com”-和Enabled-eq'$true”-和。。。“顺便说一句,我得到了Get ADUser:解析查询时出错:'OfficeSorry我丢失了
'$($office.office)
周围的单引号。”。回答更新谢谢,我想使用“emailaddress-like'*@contoso.com'-和Enabled-eq'$true'-和Office-eq'$($Office.Office)”,但Office字段返回为null