Exchange cmdlets when called from remote machine - powershell

I have one exchange server and a windows 7 machine.
W.R.T remote execution
Server - Exchange Server (Win server 2012)
Client - Win 7 machine
I want to run scripts which are present in client machine on remote machine (exchange/ win server 2012). But these are failing with error cmdlets not found.
So to check quickly i tried to invoke normal powershell cmdlets as well as exchange cmdlets and found that only exchange cmdlets are failing. However if i run same cmdlet on server (exchange) it gives me expected output.
Questions
Won't exchange cmdlets work in remote powershell ?
I tried with different session type having exchange server as connection URL but facing errors there as well.
Attached below sample test outputs.
Help me how to proceed further !!
On remote client (Win 7 machine)
PS C:\Users\Administrator> invoke-command -Session $session -ScriptBlock { ls }
returns:
Directory: C:\Users\Administrator\Documents
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
d----- 12/2/2018 12:10 PM WindowsPowerShell 10.76.68.251
But the Exchange cmdlets do not work
PS C:\Users\Administrator> invoke-command -Session $session -ScriptBlock { Get-Mailbox }
The term 'Get-Mailbox' 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: (Get-Mailbox:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : 10.76.68.251
Server - Exchange / Server 2012
PS C:\Users\Administrator\Downloads\custom scripts> Get-Mailbox
Name Alias ServerName ProhibitSendQuota
---- ----- ---------- -----------------
Administrator Administrator win-j1uti0rc7qp Unlimited
DiscoverySearchMailbox... DiscoverySearchMa... win-j1uti0rc7qp 50 GB (53,687,091,200 bytes)
Test with Exchange Server URL in Connection URI
Test 1
PS C:\Users\Administrator> $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://10.76.68.251/PowerShell/ -Authentication Kerberos -Credential $credential
Error:
New-PSSession : [10.76.68.251] Connecting to remote server 10.76.68.251 failed with the following error message : The
WinRM client cannot process the request. Kerberos authentication cannot be used when the destination is an IP address.
Specify a DNS or NetBIOS destination or specify Basic or Negotiate authentication. For more information, see the
about_Remote_Troubleshooting Help topic.
At line:1 char:13
+ $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : -2144108277,PSSessionOpenFailed
Test 2
PS C:\Users\Administrator> $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://10.76.68.251/PowerShell/ -Credential $credential
Error:
New-PSSession : [10.76.68.251] Connecting to remote server 10.76.68.251 failed with the following error message : The
WinRM client cannot process the request. It cannot determine the content type of the HTTP response from the
destination computer. The content type is absent or invalid. For more information, see the
about_Remote_Troubleshooting Help topic.
At line:1 char:13
+ $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession],PSRemotingTransportException
+ FullyQualifiedErrorId : -2144108297,PSSessionOpenFailed

Yes, they do, this is a common practice, but what you are doing is not complete enough.
PSRemoting must properly be enabled.
You must pass the
credentials of an account that is an admin on the box and admin in
Exchange
You have to use PSRemoting to do this and that is well documented by Microsoft for not only Exchange on prom but for Exchange Online.
Connect to Exchange servers using remote PowerShell
Connect-O365 1.5.4
If you are using the PowerShell ISE, you can take either of these approaches, just remember to hit that Refresh button on the Commands tabs to see the cmdlets reflected.
How To–Load Exchange Management Shell into PowerShell ISE
Adding Exchange Shell items to PowerShell ISE
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
"Connect to Exchange # Contoso", {
$ExSession= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exserver.contoso.com/PowerShell/ -Authentication Kerberos
Import-PSSession $ExSession
},
"Control+Alt+1"
)
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
"Connect to Exchange On-Premise", {
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer –auto
},
"Control+Alt+2"
)
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
"Connect to Exchange Online", {
$o365Cred= Get-Credential
$o365Session= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365Cred -Authentication Basic -AllowRedirection
Import-PSSession $o365Session
},
"Control+Alt+3"
)
If you are using the console host, just remove all the ISE stuff.
$ExSession= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exserver.contoso.com/PowerShell/ -Authentication Kerberos
Import-PSSession $ExSession

Related

Connect to Exchange server using powershell

I tried running the following script to connect to an exchange server but I get the below error. Is anything wrong with the script? Are there any changes to be done from the server end?
Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://mail.company.tld/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Error:
New-PSSession : [mail.deloitte.ca] Connecting to remote server mail.deloitte.ca failed with the following error message : The WinRM client sent a request to an HTTP
server and got a response saying the requested HTTP URL was not available. This is usually returned by a HTTP server that does not support the WS-Management protocol.
For more information, see the about_Remote_Troubleshooting Help topic.
At line:4 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : URLNotAvailable,PSSessionOpenFailed
Have you checked PowerShellVirtualDirectory as mentioned in this document? It mentions exact same issue.
Assuming your ConnectionUri is correct, most likely PowerShell remoting is not enabled on the target CAS server. You can enable it by running this on the CAS server directly:
Enable-PSRemoting
You may also need to configure the PowerShell virtual directory with:
Set-PowerShellVirtualDirectory "[ServerName]\POWERSHELL (default web site)" -BasicAuthentication $true

Office 365 - Connecting to Exchange using PowerShell WinRM issues

I am trying to connect to my O365 Exchange Online but getting WinRM error messages when doing so.
$user = "user#domain.co.uk"
$cred = Get-Credential -Credential $user
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred -Authentication "Basic" -AllowRedirection
when running that I get the following error message:
> New-PSSession : [outlook.office365.com] Connecting to remote server
> outlook.office365.com failed with the following error message : The
> WinRM client cannot process the request. Basic authentication is
> currently disabled in the client configuration. Change the client
> configuration and try the request again. For more information, see the
> about_Remote_Troubleshooting Help topic. At line:1 char:20
> + $exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -Connecti ...
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession],
> PSRemotingTransportException
> + FullyQualifiedErrorId : -2144108321,PSSessionOpenFailed
I have looked online around WinRM and basic authentication.
I have enabled basic authentication through GPO but this hasn't done much.
Any information would be great.
Thanks
Looks to me like you have MFA enabled for the admin account - disable that. Cheers.

Powershell Connect to Exchange Server failed

I would like to connect to Exchange Server 2010 by using PowerShell.
I am using the following command:
PS C:\Windows\system32> $UserCredential = Get-Credential
PS C:\Windows\system32> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://<serverName>/powershell-liveid/ -Credential $UserCredential -Authenticat
At this point I got the following error message:
WARNING: Your connection has been redirected to the following URI: "https://****************/owa/pow
ershell-liveid "
New-PSSession : [******************] Connecting to remote server **************** failed
with the following error message : The WinRM client received an HTTP status code of 440 from the remote
WS-Management service. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ht ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession],
PSRemotingTransportException
+ FullyQualifiedErrorId : -2144108273,PSSessionOpenFailed
I have tried to search for this 440 Error and figure out what is the exact cause for this error, but got no result. Could anyone please give some ideas about why it fails and how to fix it?
On all the exchange servers that I manage the uri is http://EXCHANGESERVERfqdn/PowerShell

Using webjobs to run powershell script shows "The request is not supported" error

I'm trying to run following powershell script using webjobs, but receive an error. Local run works just fine.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
The output from webjob run details is:
ERR ] New-PSSession : [outlook.office365.com] Connecting to remote server
outlook.office365.com failed with the following error message : WS-Management
cannot process the request. The operation failed because of an HTTP error. The
HTTP error (50) is: The request is not supported. . For more information, see
the about_Remote_Troubleshooting Help topic.
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri ht ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMOperationAborted,PSSessionOpenFailed
Is it possible to connect to remote Exchange online server from azurewebsites webjob and "do something" there?

Unable to connect to live#edu through PowerShell

I am trying to connect to live#edu using PowerShell with the following command.
$SessionNew = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://pod51002psh.outlook.com/powershell
Error:
[pod51002psh.outlook.com] Connecting to remote server failed with the
following error message : The WinRM client cannot process the
request. The destination computer returned an empty response to the
request. For more information, see the about_Remote_Troubleshooting
Help topic.
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [],
PSRemotingTransportException
+ FullyQualifiedErrorId : PSSessionOpenFailed
Can anyone suggest me what to do with this type of errors?
I am working with following System enviornment:
Win 7, default version of PowerShell & IIS feature enabled.
Is this Code working?
$Uri = "https://pod51002psh.outlook.com/powershell/"
$Credentials = Get-Credential
$SessionNew = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $Uri -Credential $Credentials -Authentication Basic –AllowRedirection
Import-PSSession $SessionNew
In order for Import-PSSession to work though, you will have to change local execution policy with:
Set-ExecutionPolicy remotesigned