0

我正在尝试Active Directory从特定的Organization Unitusing中获取所有电子邮件和用户名powershell。这是我的代码:

function Invoke-CmdCommand {
    Param(
        [parameter(position = 0)]
        $Cmd , 
        [parameter(position = 1)]
        $RunFolder = $PSScriptRoot
    )
    try {
        $cmdToRun = "cmd /c cd `"$RunFolder`" `"&`" $Cmd";
        Write-Host "$RunFolder> $Cmd";
        Invoke-Expression "& $($cmdToRun.Replace("^","^^"))";
    }
    catch {
        Write-Error "********** Function $($MyInvocation.MyCommand.Name) failed **********";
        Write-Error $_.Exception.Detail.InnerText;
        exit 1;
    }
}


$cmd = "dsquery user `"OU=Disabled Users,DC=microfinancial,DC=com`" -limit 10000 | dsget user -samid -email"

$test = Invoke-CmdCommand -Cmd $cmd

我收到以下错误:

dsget 失败:“此命令的目标对象”的值格式不正确。输入 dsget /? 求助。

我能做些什么?

4

2 回答 2

1

正如上面评论中所指出的,你最好使用Get-ADUser. 这将为您提供仅包含用户名和电子邮件地址的对象数组:

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=Disabled Users,DC=microfinancial,DC=com" `
    -Properties SamAccountName,EmailAddress `
  | Select-Object SamAccountName,EmailAddress

如果Import-Module ActiveDirectory对您不起作用,请安装 RSAT(假设您使用的是 Windows 10):https ://www.microsoft.com/en-ca/download/details.aspx?id=45520

或者,如果您在 Windows 的服务器版本上运行它:

Import-Module ServerManager
Add-WindowsFeature RSAT-AD-PowerShell
于 2018-10-23T15:36:45.950 回答
1

如果您想要做的只是来自 Active Directory 的电子邮件和用户名,Get-ADUser那么包含在ActiveDirectory模块中的就是首选路由。

-SearchBase将允许您确定要开始使用的 OU。您需要使用 指定要包含的Get-ADUser属性-Properites。将Select仅显示您希望使用的属性。

以下示例将获取所有用户的用户名 (samaccountname) 和主电子邮件:

Get-ADUser -filter * -Properties SamaccountName, mail -SearchBase "DC=domain,DC=local" | Select SamaccountName, Mail
于 2018-10-23T15:37:24.347 回答