2016-07-31 21 views
1

当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 

回答

1

Where-Object是一个PowerShell命令行不是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 
} 
相关问题