ADUsers2CSV脚本中的变量

时间:2017-01-31 12:42:23

标签: powershell csv active-directory export-to-csv

我的主要目标是将有关用户的一些信息从Active Directory导出到CSV文件。 但在我的AD中,还有一些我不想导出的服务帐户,我只想使用特定的OU。

所以我用这些OU的“列表”和导出的CSV的文件名创建了CSV文件,如下所示:

oubase,oucity,filename
OU=_SubCompany1,OU=_City1,filename1.csv
OU=_SubCompany1,OU=_City2,filename2.csv
OU=_SubCompany3,OU=_City1,filename3.csv

我的PS脚本:

Import-Module ActiveDirectory

$adserver = "ad1.domain.com"
$filter = "'(& (msExchMailboxGuid=*) (!sAMAccountName=*_*))'"
$selection = "personalTitle, msDS-PhoneticFirstName, msDS-PhoneticLastName, msDS-PhoneticDepartment, Title, mobile, telephoneNumber, facsimileTelephoneNumber, SamAccountName"
$targetpath = "C:\Scripts\ADUsers\export\"
$exportfiles = "C:\Scripts\ADUsers\export\export_files.csv"

Import-Csv -Path $exportfiles | ForEach-Object {
    $oubase = $_.'oubase'
    $oucity = $_.'oucity'
    $filename = $_.'filename'
    $fullpath = $targetpath + $filename

    $fullbase = "'OU=Users," + $oucity + "," + $oubase + ",DC=domain,DC=com'"
    #$fullbase = 'OU=Users,OU=_City2,OU=_SubCompany1,DC=domain,DC=com'

    Get-ADUser -Server $adserver -LDAPFilter $filter -Properties * -SearchBase $fullbase |
        select $selection |
        Export-Csv -Encoding Unicode $fullpath

但脚本无效。 Get-ADUser命令中的变量存在一些问题。 当我直接输入所有值时,一切正常:

Get-ADUser -Server ad1.domain.com -LDAPFilter '(& (msExchMailboxGuid=*) (!sAMAccountName=*_*))' -Properties * -SearchBase 'OU=Users,OU=_City2,OU=_SubCompany1,DC=domain,DC=com' |
    select personalTitle, msDS-PhoneticFirstName, msDS-PhoneticLastName, msDS-PhoneticDepartment, Title, mobile, telephoneNumber, facsimileTelephoneNumber, SamAccountName |
    Export-Csv -Encoding Unicode C:\Scripts\ADUsers\export\filename2.csv

当我尝试显示变量的内容时,似乎没问题,没有额外的空格或逗号等。

我认为变量$filter$fullbase存在问题。

当我尝试使用$filter变量时,命令不会返回任何错误,并且会创建空CSV:

Get-ADUser -Server $adserver -LDAPFilter $filter -Properties * -SearchBase 'OU=Users,OU=_City2,OU=_SubCompany1,DC=domain,DC=com' |
    select personalTitle, msDS-PhoneticFirstName, msDS-PhoneticLastName, msDS-PhoneticDepartment, Title, mobile, telephoneNumber, facsimileTelephoneNumber, SamAccountName |
    Export-Csv -Encoding Unicode C:\Scripts\ADUsers\export\filename2.csv

当我使用$fullbase变量进行尝试时,命令会返回此错误。

Get-ADUser -Server $adserver -LDAPFilter '(& (msExchMailboxGuid=*) (!sAMAccountName=*_*))' -Properties * -SearchBase $fullbase |
    select personalTitle, msDS-PhoneticFirstName, msDS-PhoneticLastName, msDS-PhoneticDepartment, Title, mobile, telephoneNumber, facsimileTelephoneNumber, SamAccountName |
    Export-Csv -Encoding Unicode C:\Scripts\ADUsers\export\filename2.csv
Get-ADUser : The supplied distinguishedName must belong to one of the following
partition(s): 'DC=domain,DC=com , CN=Configuration,DC=domain,DC=com , 
CN=Schema,CN=Configuration,DC=domain,DC=com , DC=DomainDnsZones,DC=domain,DC=com ,
DC=ForestDnsZones,DC=domain,DC=com'.
At C:\Scripts\ADUsers\export\ad-users_export-csv.ps1:21 char:1
+ Get-ADUser -Server $adserver -LDAPFilter '(& (msExchMailboxGuid=*) (!sAMAccountN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

你有什么想法我做错了吗?

0 个答案:

没有答案