PowerShell远程调用Exchange与其他凭据

我一直负责创build一个将在整个networking上的多个用户机器上运行的脚本。 我们最终要运行的命令会将用户的电子邮件别名更改为基于select的内容。 那,我没有问题。 我的问题是,用户需要访问Exchange命令以及具有使用权限的帐户。

我使用以下命令在服务器上以XML格式保存了服务帐户密码,该服务器上的凭据位于此处:

$MyCredentials=Get-Credentials -Credential "domain\username" | Export-CliXml C:\Scripts\SecureCredentials.xml 

然后在脚本中,我input了以下信息:

 $cred=Import-CLIXML \\server\Scripts\SecureCredentials.xml 

据我所知,这部分工作正常。

然后我试图导入Microsoft Exchange 2010模块,然后运行我前面提到的命令。 我写了这个小部分,得到奇怪的错误。 以下是我所编码的内容:

 $Session = New-PSsession -ComputerName exchangeserver Invoke-Command -Command {Import-Module Microsoft.Exchange.Management.PowerShell.2010} -Session $Session Import-PSSession -Session $Session -Module Microsoft.Exchange.Management.Powershell.2010 -Prefix RM 

我尝试testing该部分,并得到一个WinRM错误消息,但据我所知,WinRM在我的Exchange服务器上工作正常。 这是错误:

 New-PSsession : [exchangeserver] Connecting to remote server exchangeserver failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic. At C:\users\me\documents\Powershell Scripts\ExchangeBranding.ps1:17 char:14 + $Session = New-PSsession -ComputerName exchangeserver + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin gTransportException + FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At C:\users\me\documents\Powershell Scripts\ExchangeBranding.ps1:18 char:98 + ... 2010} -Session $Session + ~~~~~~~~ + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand Import-PSSession : Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again. At C:\users\me\documents\Powershell Scripts\ExchangeBranding.ps1:19 char:29 + Import-PSSession -Session $Session -Module Microsoft.Exchange.Management.Power ... + ~~~~~~~~ + CategoryInfo : InvalidData: (:) [Import-PSSession], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportPSSessionCommand 

当我把它叫出来导入它时,我需要服务器的完全限定名吗? 我试图在TechNet的文章中跟进,它使我了解了这一点,但是我无法导入模块。

编辑:我现在可以导入模块,但我有附加凭据的问题。 如果我没有embedded证书,只是手动导入他们,我可以得到模块导入以下内容:

 $session=New-PSSession -ConfigurationName Microsoft.Exchange.Management.Powershell.2010 -ConnectionUri http://exchangeserver.domain.com/powershell 

之前有一个类似的问题 – 这是远程主机上的一个简单的权限问题。 我不知道了,但我想我使用这个小指南: http : //blogs.msdn.com/b/powershell/archive/2009/11/23/you-don-t-have-to-be-一个pipe理员对运行远程PowerShell的commands.aspx

而且我也build议将所有东西都打包到可执行文件中(使用PS2EXE – https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1 )。 通过这一点,你可以破解你的脚本硬编码的凭据,并避免一个XML文件丢失,并把它给你的用户,而不必担心它了。

所以我有多个东西出错。 感谢所有回答的人,这实际上让我走上了正确的道路。

我不得不在我的一个命令的末尾删除-Credential $ session标志。 它没有正确parsing,凭据已经从上面的命令parsing到窗口中。 我在“-ConnectionUri”前面也有一个领先的空间,这个空间正在引起合适。

最终代码如下:

 $cred=Import-CLIXML \\server\Scripts\SecureCredentials.xml $session=New-PSSession -ConfigurationName Microsoft.Exchange.Management.Powershell.2010 -ConnectionUri http://server.domain.com/powershell Import-PSSession $session