使用PowerShell从AD导入用户

使用PowerShell从AD导入用户,powershell,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, lo

我是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
我得到的错误如下:

#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

我不确定我在脚本中的错误在哪里,但如果有任何帮助,我将不胜感激。

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

Get ADUser需要一个筛选器参数,您没有提供该参数。尝试将-Filter*添加到您的通话中,以获得ADUser,并查看这会给您带来什么。请注意,这不是很有效,因为您正在获取所有AD用户对象,然后使用Where对象对其进行过滤。阅读有关Get ADUser的详细帮助,了解如何将Where对象过滤器转换为Get ADUser所需的格式;然后,您将在查询广告时进行筛选,这会更好。此外,PowerShell错误消息往往对确定问题的确切原因非常有帮助。您的问题在错误消息中完全正确。无法验证参数“Filter”上的参数。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令。