Mitchell Matula
Mitchell Matula

Reputation: 35

Invoking-Command for Exchange in Powershell - block is not allowed in a Data section

What I'm trying to do is run this script:

$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)
})

But it's giving me the error:

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

I'm running the whole command from a button click in a XAML Powershell GUI. I googled alot trying to solve the problem as I usually do but no luck :(

Any help would be GREATLY appreciated.

Upvotes: 2

Views: 2153

Answers (2)

Gholie
Gholie

Reputation: 11

Bit of an old bump here, but seeing as it is rather high on Google search I figured I could add how I fixed this issue:

$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)

This imports the Exchange Commands as you would in a local session. The Exchange commands will still be executed on the remote server. Remember to close the session when done.

Upvotes: 1

beatcracker
beatcracker

Reputation: 6920

It looks like Exchange uses restricted language mode for remote sessions and you can't execute scriptblocks in your session.

As a security feature, the language mode is obviously controlled at the server (Exchange), so if you want to enable execution of scriptblocks you need to interactively logon (RDP or console) to Exchange and create a new session configuration via Register-PSSessionConfiguration. You may then connect to Exchange using this session configuration via New-PSSession -ConfigurationName and you will then be able to execute scriptblocks by passing this session instance to Invoke-Command -Session.

Reference:

Upvotes: 1

Related Questions