在powershell中使用get-aduser指定域控制器

Aar*_*ron 6 powershell active-directory

Get-ADUser -identity $ntaccount1 -properties name, samaccountname, mail, enabled, passwordlastset
Run Code Online (Sandbox Code Playgroud)

在powershell中查找用户帐户信息时,是否可以指定要使用的域控制器?我们有一些DC比其他DC更快地获得数据.

mjo*_*nor 14

来自Get-Help Get-ADUser -Parameter*

-Server <string>
    Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a 
    corresponding domain name or directory server. The service may be any of the following:  Active Directory Lightweight Domain 
    Services, Active Directory Domain Services or Active Directory Snapshot instance.
    Domain name values:
      Fully qualified domain name
        Examples: corp.contoso.com
      NetBIOS name
        Example: CORP

    Directory server values:
      Fully qualified directory server name
        Example: corp-DC12.corp.contoso.com
      NetBIOS name
        Example: corp-DC12
      Fully qualified directory server name and port
        Example: corp-DC12.corp.contoso.com:3268

    The default value for the Server parameter is determined by one of the following methods in the order that they are listed:
      -By using Server value from objects passed through the pipeline.
      -By using the server information associated with the Active Directory PowerShell provider drive, when running under that drive.
      -By using the domain of the computer running Powershell. 

    The following example shows how to specify a full qualified domain name as the parameter value.
      -Server "corp.contoso.com"

    Required?                    false
    Position?                    named
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false
Run Code Online (Sandbox Code Playgroud)


小智 11

我知道这是一个古老的问题,但我想扩大给出的答案,以帮助其他有类似疑问的人.

以下允许您定义一个特定的域控制器,整个脚本都可以使用...当-server参数可用于Get-ADUser,New-ADUser,Set-时,为什么要这样做? ADObject等?

好吧,我整理了一个创建AD用户,设置多个属性并创建交换邮箱的脚本 - 但是,一组属性围绕2008 R2用户帐户上的RDS属性,这些属性无法在New-ADUser中设置.我必须创建一个调用ADSI的函数,并使用psbase.invokeSet来更新设置.我知道-server没有参数设置.

这本身并不是什么大问题,但该脚本还为用户创建了一个Exchange邮箱.由于我的Exchange服务器与我的工作站位于不同的AD站点中,因此在我的本地DC上创建了用户帐户,但未设置邮箱,因为与Exchange服务器位于同一站点的DC尚未收到复制副本新用户帐户.

我找到的解决方案如下,并且是http://www.joseph-streeter.com/?p=799的礼貌

加载了import-module activedirectory后,您将可以访问New-PSDrive命令行开关中的AD选项,这些选项允许您定义要使用的新Active Directory提供程序.

New-PSDrive -Name <<NameofYourChoice>> -PSProvider ActiveDirectory -Server <<DC Server>> -Root "//RootDSE/" -Scope Global
Run Code Online (Sandbox Code Playgroud)

创建后,您可以使用以下命令更改工作提供程序.

CD <<NameofYourChoice>>:
Run Code Online (Sandbox Code Playgroud)

要查看现有的提供程序列表,请键入Get-PSDrive.AD是使用ActiveDirectory命令行开关时创建的默认Active Directory提供程序.您还应该看到新创建的提供商.

因此,例如,如果我的远程DC被称为RemoteDC,我将运行:

New-PSDrive -Name RemoteAD -PSProvider ActiveDirectory -Server RemoteDC -Root "//RootDSE/" -Scope Global
Run Code Online (Sandbox Code Playgroud)

创建一个名为RemoteAD的新Provider.如果我然后运行:

CD RemoteAD:
Run Code Online (Sandbox Code Playgroud)

脚本或活动shell中的所有其他与活动目录相关的命令都将与新的Provider RemoteAD一起使用.如果我需要更改回原始提供商,我只需输入

CD AD:
Run Code Online (Sandbox Code Playgroud)

希望有人觉得这很有用......