使用PowerShell从AD导入用户

时间:2013-10-02 19:14:00

标签: powershell

我是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

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

1 个答案:

答案 0 :(得分:0)

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