2

I have one PS script that need to be execute/invoke via XML. getting error like

$sessions = Get-RDUserSession foreach($session in $sessions){Invoke- ...

Unexpected token 'in' in expression or statement. At line:1 char:78 $sessions = Get-RDUserSession foreach($session in $sessions){Invoke- ...
Missing closing ')' in expression. At line:1 char:109

config.xml

<Action name="KillAllUsers" Type="Powershell" Executor='$sessions = Get-RDUserSession
foreach ( $session in $sessions ) 
{ Invoke-RDUserLogoff -HostServer $session.HostServer -UnifiedSessionID $session.UnifiedSessionId -Force }'></Action>

1 Answers1

0

Literal[1] newlines (line breaks) in XML attributes of XML text are seemingly not preserved when XML text is parsed into a DOM (Document Object Model); they are replaced with spaces.

Therefore, whatever code you put in your Executor attribute ends up as a single line, which is why you must ;, PowerShell's statement separator, to separate your statements; note the ; after Get-RDUserSession and note that the line breaks are purely for readability here:

<Action name="KillAllUsers" Type="Powershell" Executor='
$sessions = Get-RDUserSession;
foreach ( $session in $sessions ) { 
  Invoke-RDUserLogoff -HostServer $session.HostServer -UnifiedSessionID $session.UnifiedSessionId -Force
}
'></Action>

As for the error you saw:

Due to your code being placed all on a single line without statement separators, the foreach statement syntactically became an argument to the Get-RDUserSession command, which caused a syntax error; a simple way to provoke the error is:

# -> ERROR: "Unexpected token 'in' in expression or statement."
Get-Date foreach ($i in 1..2) { $i }

[1] By contrast, escaped line breaks (&#xA; for LF-only newlines, &#xD;&#xA; for CRLF newlines) are preserved: they are converted to literal newlines in memory (in the DOM), and reconverted to their escaped form during serialization, i.e. when converting the in-memory DOM to a string or saving it to a file.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks, I have past that error but getting something different error now. – Willingtolearn Oct 17 '22 at 13:57
  • Glad to hear it, @Willingtolearn. As for a different error: unless it is a simple thing that can quickly be resolved in the comments here, I suggest creating a _new_ question post. – mklement0 Oct 17 '22 at 14:01
  • 1
    thank you very much. I am not sure, if its a easy fix but I can create a new post :) A Remote Desktop Services deployment does not exist on server. This operation can be performed after creating a deployment. For information about creating a deployment, run "Get-Help New-RDVirtualDesktopDeployment" or "Get-Help New-RDSessionDeployment". – Willingtolearn Oct 17 '22 at 14:03