无法使用 Active Directory 运行远程 Powershell

Mar*_*ark 5 remote-access powershell active-directory

我们正在尝试通过 Powershell 连接到远程服务器并使用 ActiveDirectory 模块。尝试在本地执行此操作时,一切似乎都很好。

PS C:\Users\bar> Import-Module ActiveDirectory
PS C:\Users\bar> Get-ADUser 'baz'

DistinguishedName : CN=Foo Baz,OU=baz.myhost.com,OU=FooMachine,DC=foo,DC=blah,DC=loc
Enabled           : True
GivenName         : Baz
Name              : Foo Baz
ObjectClass       : user
ObjectGUID        : <some guid>
SamAccountName    : baz
SID               : <more info here>
Surname           : Baz
UserPrincipalName : baz@foo
Run Code Online (Sandbox Code Playgroud)

当我们远程做同样的事情时,我们就没有那么幸运了。

C:\> Enter-PSSession -ComputerName 172.1.2.3 -Credential foo\bar
[172.1.2.3]: PS C:\Users\bar\Documents> Import-Module ActiveDirectory
WARNING: Error initializing default drive: 'Unable to contact the server. This
may be because this server does not exist, it is currently down, or it does not
 have the Active Directory Web Services running.'.
[172.1.2.3]: PS C:\Users\bar\Documents> Get-ADUser 'baz'
Unable to contact the server. This may be because this server does not exist, i
t is currently down, or it does not have the Active Directory Web Services runn
ing.
    + CategoryInfo          :
    + FullyQualifiedErrorId : Unable to contact the server. This may be becaus
   e this server does not exist, it is currently down, or it does not have th
  e Active Directory Web Services running.,Microsoft.ActiveDirectory.Managem
 ent.Commands.GetADUser

[172.1.2.3]: PS C:\Users\bar\Documents>
Run Code Online (Sandbox Code Playgroud)

Christopher,我们有 2 - 2008 R2 域控制器在该域中运行。Active Directory Web 服务在两者上都运行(“Import-Module ActiveDirectory”在服务器控制台上运行良好 - 顺便说一下,它不是域控制器

chu*_*dde 7

以下是使用 CredSSP 解决类似问题的示例。我对此进行了测试,它可以解决您在问题中发布的 AD Web 服务错误。

总结本文,首先您需要在客户端和服务器上启用 CredSSP。

在客户端: Enable-WSManCredSSP -Role Client -DelegateComputer [computer name] -Force

在服务器上: Enable-WSManCredSSP -Role Server –Force

接下来,您需要获取或制作凭据以连接到另一台机器并创建一个使用该凭据的会话。然后,您可以使用Invoke-Command在该新会话的脚本块中运行 PowerShell 命令/脚本。这是文章中的部分示例,使用您问题中的命令:

$credential = Get-Credential -Credential iammred\administrator

$session = New-PSSession -cn SQL1.Iammred.Net -Credential $credential -Authentication Credssp

Invoke-Command -Session $session -ScriptBlock { Import-Module ActiveDirectory; Get-ADUser 'baz' }
Run Code Online (Sandbox Code Playgroud)

但是,这会以交互方式要求您提供凭据,因此如果您想避免这种情况,则需要执行以下操作$credential

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "DOMAIN\username",$pass;

其中$pass是与帐户关联的密码的安全字符串。


小智 5

我在我们的几个环境中遇到了同样的问题,有效的方法是更改​​防火墙。显然,ADWS 使用端口 9389,试图使用 powershell 远程管理 DC 的服务器不允许使用该端口。一旦我们允许端口,一切都会顺利进行。


小智 3

在这种情况下是否需要 CREDSSP?