[英]Error when using powershell while remoting in Exchange 2010
我正在尝试编写一些入职流程的脚本,并为此尝试启用交换邮箱。 我有一些行似乎不在脚本内工作,但在脚本内引发错误。 你能帮我吗?
代码如下:
Function enableExchangeMailbox {
#Grabs admin credentials from xml document and imports it, setting the variable
$UserCredential = Import-Clixml 'SecureCredentials.xml'
#Sets up a new remote session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mxex2010.minnetronix.com/Powershell -Authentication Kerberos -Credential $UserCredential
#Enable the mailbox on the server
Invoke-Command -Session $Session -ScriptBlock {
Enable-Mailbox -Identity $global:userName -Database $global:exchangeDatabase
}
#cleanup
Remove-PSSession $Session
}
它引发的错误是这样的:
“身份”的全局变量是在脚本的前面设置的,并且可以在我的其他函数中使用。 任何帮助,将不胜感激。
Invoke-Command
的scriptblock参数将在远程计算机上运行,并且将无法访问调用方的全局范围。
将$userName
作为参数传递给函数,并using:
限定符将其传递给远程会话:
Function enableExchangeMailbox {
param($userName)
#Grabs admin credentials from xml document and imports it, setting the variable
$UserCredential = Import-Clixml 'SecureCredentials.xml'
#Sets up a new remote session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mxex2010.minnetronix.com/Powershell -Authentication Kerberos -Credential $UserCredential
#Enable the mailbox on the server
Invoke-Command -Session $Session -ScriptBlock {
Enable-Mailbox -Identity $using:userName -Database $global:exchangeDatabase
}
#cleanup
Remove-PSSession $Session
}
像这样的电话:
enableExchangeMailbox -userName $userName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.