1

次のコマンドを 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

このコマンドレットをこの「セッション」に追加したり、この問題を回避するにはどうすればよいですか?

背景: winrm を使用して ruby​​ スクリプトを使用してリモートでいくつかのことを自動化しようとしていますが、Exchange Powershell の「レイヤー」に接続するための他の方法は見つかりませんでした。

「実際の」Exchange 管理シェルでは、このコマンドは正常に機能します (したがって、PowerShell のインストール全体が壊れているとは思いません (Module Microsoft.PowerShell.Utility))。「New-PSSession」と「Invoke-Command」を介してこのシェルに接続する方法の方が多いと思います。

それが役立つ場合は、ここにルビーコードがあります(ただし、これはリモートで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
4

1 に答える 1

1

Where-ObjectExchangeコマンドではなくPowerShellコマンドです...

Exchange への新しいセッションを作成すると、Exchange コマンドレットのみが含まれるためWhere-Object、それらの 1 つではありません。

できることは、セッションをインポートしてから、現在のセッションで実行することです。例:

$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
}
于 2016-07-31T14:23:47.957 に答える