1

Lorsque iam exécutant la commande suivante localement sur mon serveur Exchange,à distance Powershell: Le terme 'Where-Object' est pas reconnu dans "Invoke-Command"

$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} 

je reçois ce message d'erreur:

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 

Comment ajouter cette cmdlet à cette "session" ou résoudre ce problème? J'essaye d'automatiser certaines choses à distance avec un script ruby ​​en utilisant winrm et je n'ai trouvé aucune autre alternative pour me connecter au "Layer" d'Exchange Powershell.

Sur le "vrai" Exchange Management Shell, cette commande fonctionne bien (donc je ne pense pas que toute l'installation de PowerShell est cassée (Module Microsoft.PowerShell.Utility)). Je pense que c'est plus la façon dont je me connecte à ce shell sur les choses "New-PSSession" et "Invoke-Command".

Si elle aide, voici le code Ruby (mais je pense que cela n'a rien à voir avec la façon dont je me connecte au powershell à distance - mais s'il y a une bien meilleure façon, laissez-moi, s'il vous plaît):

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 

Répondre

1

Where-Object est une commande PowerShell pas une commande d'échange ...

Lorsque vous la création d'une nouvelle session à Exchange, il ne comprend que la Bourse Cmdlets, donc Where-Object n'est pas un d'entre eux.

Ce que vous pouvez faire est d'importer la session, puis l'exécuter dans la session en cours, par exemple:

$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 

puis exécutez:

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

Cependant, cela ne fonctionnera pas:

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