当我在Exchange Server上本地运行以下命令时,
$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安装已损坏(模块Microsoft.PowerShell.Utility))。我认为它更像是我通过“New-PSSession”和“Invoke-Command”事物连接到这个shell的方式。
如果它有帮助,这里是红宝石代码(但我认为这与我远程连接到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
答案 0 :(得分:1)
Where-Object
是PowerShell命令而非Exchange命令...
创建新的Exchange会话时,它只包含Exchange Cmdlet,因此Where-Object
不是其中之一。
您可以做的是导入会话,然后在当前会话中运行它,例如:
$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
Import-PSSession $Session
然后运行它:
Get-Mailbox -RecipientTypeDetails 'SharedMailbox' | Get-MailboxPermission | Where-Object User -like 'anotherusername' | fl identity
这不会起作用:
Invoke-Command -Session $Session -ScriptBlock {
Get-Mailbox -RecipientTypeDetails 'SharedMailbox' | Get-MailboxPermission | Where-Object User -like 'anotherusername' | fl identity
}