【问题标题】:Error when using powershell while remoting in Exchange 2010在 Exchange 2010 中进行远程处理时使用 powershell 时出错
【发布时间】:2017-04-27 21:21:25
【问题描述】:

我正在尝试为我们的一些入职流程编写脚本,并且我正在尝试启用邮箱作为交换。我有一些似乎在脚本之外工作的行,但在脚本内部抛出错误。你能帮帮我吗?

代码如下:

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
}

它抛出的错误是这样的:

“身份”的全局变量已在脚本的前面设置,并且可以在我的其他函数中使用。任何帮助,将不胜感激。

【问题讨论】:

    标签: powershell scripting exchange-server powershell-remoting exchange-server-2010


    【解决方案1】:

    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
    

    【讨论】:

    • 工作就像一个魅力。谢谢你!
    猜你喜欢
    • 2011-07-02
    • 2020-06-10
    • 2019-04-19
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多