rubyshellpowershellexchange-serverexchange-server-2013

Remote Powershell: The term 'Where-Object' is not recognized in "Invoke-Command"


When iam running the following command locally on my 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}

i get this error message:

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

How could i add this cmdlet to this "session" or get arround this problem?

Background: Iam trying to automate some stuff remotely with a ruby script using winrm and havent found any other alternative to connect to the Exchange Powershell "Layer".

On the "real" Exchange Management Shell, this command works fine (so i dont think the whole powershell installation is broken (Module Microsoft.PowerShell.Utility)). I think its more the way i connect to this shell over the "New-PSSession" and "Invoke-Command" thing.

If it helps, here is the ruby code (but i think this has nothing to do with the way i connect to the powershell remotely - but if there is a much better way, let me know, please):

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

Solution

  • Where-Object is A PowerShell Command not an Exchange Command...

    When you creating a new Session to Exchange, it includes only the Exchange Cmdlets, so Where-Object is not one of them.

    What you can do is to Import the Session, then Run it in your current session, For Example:

    $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
    

    Then Run it:

    Get-Mailbox -RecipientTypeDetails 'SharedMailbox' | Get-MailboxPermission | Where-Object User -like 'anotherusername' | fl identity
    

    This However will not work:

    Invoke-Command -Session $Session -ScriptBlock {
    Get-Mailbox -RecipientTypeDetails 'SharedMailbox' | Get-MailboxPermission | Where-Object User -like 'anotherusername' | fl identity
    }