Powershell导出CSV错误

时间:2013-10-04 11:35:44

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

只是想知道你是否可以帮助我。我收到以下错误:

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\PSScripts\CompassADStaff.ps1:33 char:20
+ $Staff | Export-Csv <<<<  CompassADStaff.csv -NoTypeInformation
+ CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

我的代码似乎没问题,但我似乎无法检测为什么export-csv会产生上述错误:

Import-Module ActiveDirectory

#-------- Object variables ----------------------------------------------

#OU's to search
$searchbase = "ou=staff1,ou=users,dc=home,dc=local","ou=staff2,ou=users,dc=home,dc=local"

$Staff = @()

#------------------------------------------------------------------------


foreach ($ou in $searchbase) {
$Staff += get-aduser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(mail=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))" -SearchBase $ou -properties * | select-object employeeID,givenName,sn,sAMAccountName,mail
}
$Staff | Export-Csv CompassADStaff.csv -NoTypeInformation  

任何帮助或指示都会很棒:)

由于

2 个答案:

答案 0 :(得分:2)

这始终为我解决了同样的问题:

$Staff | Where {$_} | Export-Csv CompassADStaff.csv -NoTypeInformation 

答案 1 :(得分:0)

可以肯定的是,您是否可以检查$ staff中是否存在数据:

If ($Staff) {$Staff | Export-Csv CompassADStaff.csv -NoTypeInformation }