1

我是 powershell 新手,需要帮助解决我的脚本中的一些错误。我基本上是在尝试从 Active Directory 导入所有用户,我的脚本如下:

#Location the CSV file is being saved.
$csvFileLocation = "C:\ADImport\ADImport.csv"

#Verifies that the AD Module is loaded.  If it is not, checks if it is available, if it is, loads it.

Function Get-ADModule {
    if(-not(Get-Module -Name ActiveDirectory)) {
        if(Get-Module -ListAvailable | Where-Object {$_.Name -eq "ActiveDirectory" }) {
            Import-Module -Name ActiveDirectory
            $true
        } else {
            $false
        }
    } else {
        $true
    }
}

if (Get-ADModule) {
    #Creates the CSV Table Format, change the AD DS TableNames to the CoarseMail TableNames
    $tableFormat = @{ Expression = { $_.GivenName -replace ","," " };Label = "FirstName" },
                   @{ Expression = { $_.SurName -replace ","," " };Label = "LastName" },
                   @{ Expression = { $_.SAMAccountName -replace ","," " };Label = "ID" }
    #Gets AD User Objects that are enabled, and only specified properties for perfomance, filters out specified OU's, filters out objects with no first or last name or EmployeeID.
    Get-ADUser -SearchBase "DC=mydomain,DC=com" -Properties SAMAccountName, GivenName, Surname `
    | Where-Object {
         ($_.GivenName -ne $null) -and
         (($_.GivenName.Length -ne 0) -and ($_.SurName.Length -ne 0))
      } `
    | Select-Object $tableFormat | Export-Csv
    $csvFileLocation -NoType
} else {
    Add-Content ($csvFileLocation + ".txt") "The ActiveDirecotry Powershell Module does not
    exist on this machine."
}

我得到的错误如下:

PS C:\Users\benc> C:\Users\benc\Desktop\ADImportTest.ps1
Get-ADUser : Cannot validate argument on parameter 'Filter'. The argument is null or empty.
Supply an argument that is not null or empty and then try the command again.
At C:\Users\benc\Desktop\ADImportTest.ps1:27 char:15
+     Get-ADUser <<<<  -SearchBase "DC=mydomain,DC=com" `
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

我不确定我在脚本中哪里出错了,但我们将不胜感激。

4

1 回答 1

0

检查Get-ADUser的语法。-Filter是必需参数。添加-Filter {objectClass -eq 'user'},错误就会消失。

于 2013-10-03T04:13:20.967 回答