我正在尝试通过 ruby 脚本为 Active Directory 中的现有用户运行 enable-mailbox 命令。我正在使用 this winrm gem .到目前为止,我已经能够使用 winrm 和 kerberos 身份验证连接到交换服务器。我可以从 powershell 运行交换管理 shell。我可以从那里执行交换命令。
但是,当我尝试运行 enable-mailbox 时,出现以下错误:
Active Directory operation failed on . The supplied credential for 'domain\account' is invalid.
“操作失败。”是逐字的。你认为应该有的地方没有文字。域\帐户与我用来通过 kerberos 成功连接 winrm 的域\帐户相同。
这是我的简单代码:
endpoint = 'http://server:5985/wsman'
krb5_realm = 'myrealm'
winrm = WinRM::WinRMWebService.new(endpoint, :kerberos, :realm => krb5_realm)
#exch_cmd = "Get-Help Enable-Mailbox" NOTE THAT THIS COMMAND WORKS FINE
exch_cmd = "Enable-Mailbox -Identity:'user DN' -Alias:'username' -Database:'mailbox'"
command = "powershell -psconsolefile \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\bin\\exshell.psc1\" -command \". "+exch_cmd+"\""
winrm.cmd(command) do |stdout, stderr|
STDOUT.print stdout
STDERR.print stderr
end
感谢您的帮助!
最佳答案
我们设法让它发挥作用。我必须先连接到“管理”服务器才能启动 powershell 命令。
endpoint = 'http://YOURSERVER:5985/wsman'
krb5_realm = '你的领域'
winrm = WinRM::WinRMWebService.new(endpoint, :kerberos, :realm => krb5_realm)
然后我不得不将交换命令修改为:
exch_cmd = "Enable-Mailbox -Identity:'DOMAIN/OU/#{fullname}' -Alias:'#{username}' -Database:'#{MailboxDB}'"
command = "powershell -NonInteractive -WindowStyle Hidden -command\"$username = '#{account}'; $password = ConvertTo-SecureString '#{password}' -asplaintext -force; $UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri #{server} -Authentication Kerberos -Credential $UserCredential;调用命令-Session $Session {#{exch_cmd}}\""
在管理和 Exchange 服务器上,服务帐户需要在远程管理组中。您还需要根据本指南更新 SDDL:http://www.sevecek.com/Lists/Posts/Post.aspx?ID=280根据您的服务器配置,这将有所不同。
关于ruby - 使用 Ruby/WinRM 从 Linux 运行 Exchange Powershell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30492804/