【问题标题】:Invoking-Command for Exchange in Powershell - block is not allowed in a Data section在 Powershell 中调用 Exchange 命令 - 数据部分中不允许使用块
【发布时间】:2015-10-28 21:55:04
【问题描述】:

我想要做的是运行这个脚本:

$WPFcmdCreateNewUser.Add_Click({

$ScriptBlockContent = {
    param ($first,
        $last,
        $upn,
        $ou,
        $password
    )
    $encryptedpass = ConvertTo-SecureString -AsPlainText $password -Force
    New-RemoteMailbox -Name $name -OnPremisesOrganizationalUnit $ou -UserPrincipalName $upn -FirstName $first -LastName $last -Password $encryptedpass -ResetPasswordOnNextLogon $false
}
    $ex = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://SERVERNAME/PowerShell/
    Invoke-Command -Session $ex -ScriptBlock $ScriptBlockContent -ArgumentList ($WPFtxtNewFirstName.Text, $WPFtxtNewLastName.Text, $WPFtxtNewAlias.Text, $WPFcboNewOU.SelectedItem.Content, $WPFtxtNewPassword.Text)
})

但它给了我错误:

ERROR: A Begin statement block, Process statement block, or parameter statement is not allowed in a Data section.
ERROR:     + CategoryInfo          : ParserError: (:) [], ParseException
ERROR:     + FullyQualifiedErrorId : InvalidScriptBlockInDataSection
ERROR:     + PSComputerName        : SERVERNAME

我正在通过单击 XAML Powershell GUI 中的按钮运行整个命令。我用谷歌搜索了很多,试图像往常一样解决这个问题,但没有运气:(

任何帮助将不胜感激。

【问题讨论】:

    标签: powershell exchange-server powershell-remoting


    【解决方案1】:

    看起来 Exchange 对远程会话使用受限语言模式,您无法在会话中执行脚本块。

    作为一项安全功能,语言模式显然控制在 服务器 (Exchange),所以 如果您想启用 您需要以交互方式登录(RDP 或控制台)的脚本块 通过交换并创建新的会话配置 Register-PSSessionConfiguration。 然后您可以连接到 Exchange 通过 New-PSSession -ConfigurationName 使用此会话配置 然后你就可以通过传递这个来执行脚本块 会话实例到 Invoke-Command -Session。

    参考:

    【讨论】:

    • 错误:New-PSSession:[SERVERNAME] 连接到远程服务器 SERVERNAME 失败,并显示以下错误消息:WS-Management 服务无法处理错误:请求。在 WSMan: SERVERNAME 计算机上的驱动器中找不到脚本会话配置。有关详细信息,请参阅错误:about_Remote_Troubleshooting 帮助主题。
    • 感谢您的帮助!所以我尝试了这个,并注册了一个新的 PSSessionConfiguration,当我尝试使用它时,这就是我现在得到的错误。我认为这是因为它在 Powershell 2.0 上......但我尝试使用 Set-PSSessionConfiguration -Name Scripting -PSVersion 3.0,它没有说 -PSVersion 这样的东西。它是 2010 年的交换管理控制台......为什么有想法?
    【解决方案2】:

    这里有点老问题,但鉴于它在 Google 搜索中的排名很高,我想我可以添加解决此问题的方法:

    $DomainCredential = (Get-Credential)
    $fqdn = "<The fully qualified domain name of the target server>"
    
    #Creates a session to the Exchange Remote Management Shell so that we can run Exchange commands. Use https:// if you have a proper setup with certificates. ( Mine was in test env )
    $ConfigSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$fqdn/powershell `
              -Credential $DomainCredential -Authentication Kerberos
    
    #Imports the module that exists in the session, in this case, Exchange Management -AllowClobber gives the imported commands presedence.
              Import-Module (Import-PSSession $ConfigSession -AllowClobber)
    

    这会像在本地会话中一样导入 Exchange 命令。 Exchange 命令仍将在远程服务器上执行。完成后记得关闭会话。

    【讨论】:

      猜你喜欢
      • 2011-03-16
      • 1970-01-01
      • 2021-11-27
      • 2011-08-27
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多