当IAM本地运行以下命令我的Exchange服务器上,远程PowerShell:“位置对象”不“调用命令”公认的术语
$username = 'username'; $password = ConvertTo-SecureString 'password' -asplaintext -force; $UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'http://exchange.myfirm.local/PowerShell' -Authentication Kerberos -Credential $UserCredential; Invoke-Command -Session $Session {Get-Mailbox -RecipientTypeDetails 'SharedMailbox' | Get-MailboxPermission | Where-Object User -like 'anotherusername' | fl identity}
我收到此错误信息:
The term 'Where-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Where-Object:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : exchange.myfirm.local
我该如何将此cmdlet添加到此“会话”中或得到此问题的周围?
背景: Iam试图通过使用winrm的ruby脚本远程自动化一些东西,并没有找到任何其他连接到Exchange Powershell“Layer”的替代方法。
在“真正的”Exchange命令行管理程序中,此命令正常工作(所以我不认为整个PowerShell安装已损坏(Module Microsoft.PowerShell.Utility))。我认为它更多的是通过“New-PSSession”和“Invoke-Command”连接到这个shell。
如果有帮助,这里是ruby代码(但我认为这与我远程连接到powershell的方式无关 - 但如果有更好的方法,请让我知道):
require 'winrm'
$exch_user = 'username'
$exch_password = 'password'
endpoint = 'http://ipaddress:5985/wsman' # this is the connection URI to the Exchange
connectionuri = 'http://exchange.myfirm.local/PowerShell' # keep in mind that the PowerShell URI MAY have to be a FQDN
winrm = WinRM::WinRMWebService.new(endpoint, :plaintext, :user => $exch_user, :pass => $exch_password, :basic_auth_only => true)
executor = winrm.create_executor
exch_cmd = "Get-Mailbox anotherusername | Select-Object -expand EMailAddresses alias"
command = "powershell -NonInteractive -WindowStyle Hidden -command \" $username = '#{$exch_user}'; $password = ConvertTo-SecureString '#{$exch_password}' -asplaintext -force; $UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri '#{connectionuri}' -Authentication Kerberos -Credential $UserCredential; Invoke-Command -Session $Session {#{exch_cmd}}\""
#$i = 0
$a ||= Array.new
executor.run_cmd(command) do |stdout, stderr|
stdout.each_line do |l|
$a << l[21..-1] if l.match(/^AddressString/)
end
#STDOUT.print "#{stdout}"
#STDERR.print stderr
#STDOUT.print stdout.class #string
#STDOUT.print stdout.scan 'AddressString'
#$i += 1
end
puts $a.sort