简体   繁体   中英

Using invoke-command scriptblock with exchange management shell

I've written some scripts to automate some user add and user modifications functions. But there is one last piece to the puzzle I can't figure out.

I need to run some commands in exchange management shell on the exchange server from a local powershell session, like an invoke-command scriptblock.

Is this possible?

Will adding Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

to the beginning of my script block accomplish this?

As for this...

Will adding Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

You can't do this natively, without installing the EMC directly on your host.

Secondly, there is no real reason to. You can use PSRemoting to proxy the Exchange cmdlets to your host. The cmdlets are only available during the session

This process is the same whether you are using Exchange on-prem or Exchange online, though Exchange Online points to the O365 URI.

This has been documented in several places via the MS provided docs as noted here:

Connect to Exchange servers using remote PowerShell

Connect to a remote Exchange server

1.On your local computer, open Windows PowerShell, and run the following command:

$UserCredential = Get-Credential

In the Windows PowerShell Credential Request dialog box that opens, enter your user principal name (UPN) (for example, chris@contoso.com) and password, and then click OK.

2.Replace with the fully qualified domain name of your Exchange server (for example, mailbox01.contoso.com) and run the following command:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos -Credential $UserCredential

Note: The ConnectionUri value is http, not https.

3.Run the following command:

Import-PSSession $Session -DisableNameChecking

https://docs.microsoft.com/en-us/powershell/exchange/exchange-server/connect-to-exchange-servers-using-remote-powershell?view=exchange-ps

Remote PowerShell in Exchange 2013

1.On your local computer, open Windows PowerShell and execute the following command:

$UserCredential = Get-Credential

2.After entering the credentials prompted when you executed the above command, execute the following:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<FQDN of Exchange 2013 Client Access server>/PowerShell/ -Authentication Kerberos -Credential $UserCredential

3.Finally, import the session established above with the following:

Import-PSSession $Session

https://blogs.technet.microsoft.com/nathanscott/2015/06/14/remote-powershell-in-exchange-2013/

Yes you can, some commands have to be run locally, below is an example. You can do the same with Exchange.

Invoke-Command -ComputerName $srv -ScriptBlock{Add-PSSnapin Microsoft.Forefront.Filtering.Management.Powershell; Get-EngineUpdateInformation}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM