为Powershell ISE引入Exchange powershell module
为Powershell ISE导入Exchange Powershell模块
使用Powershell ISE在执行命令时可以更方便,也有一定的错误提示功能,比直接在Exchange Powershell上运行会更友好。
添加 Exchange Management snapin 对 Exchange 2013/2016/2019
管理员方式打开Powershell ISE
PS C:\> Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
#如果提示如下错误,则说明该主机上没有安装Exchange的管理工具(安装Exchange管理工具需要使用Exchange的可执行性文件进行安装,本文不阐述)。
PS C:\Windows\system32> Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Add-PSSnapin : 没有为 Windows PowerShell 版本 5 注册管理单元。
所在位置 行:1 字符: 1
+ Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Excha...werShell.SnapIn:String) [Add-PSSnapin],PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
PS C:\Windows\system32> Add-PSSnapIn -Name Microsoft.Exchange
Add-PSSnapIn : 没有为 Windows PowerShell 版本 5 注册管理单元。
所在位置 行:1 字符: 1
+ Add-PSSnapIn -Name Microsoft.Exchange
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Exchange:String) [Add-PSSnapin],PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
使用Powershell ISE连接远程Exchange
#检查是否开启了powershell执行权限
PS C:\> Get-ExecutionPolicy
PS C:\> Set-ExecutionPolicy RemoteSigned
#输入远程Exchange服务器的连接秘钥
$UserCredential = Get-Credential
#填写相应的连接信息
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EX01-2016.exoip.local/PowerShell/ -Authentication Kerberos -Credential $UserCredentia
#Import the Exchange cmdlets in the Windows PowerShell session so you can administer Exchange.
PS C:\> Import-PSSession $Session -DisableNameChecking
#Verify it’s working
PS C:\> Get-Mailbox
#结束时关闭管理会话
PS C:\> Remove-PSSession $Session