here-string with $($var) text

hi all,

I need to create an XML file that contains this line into it;

<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>

my script creates the file like so;

$xmlContent = @"

<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>

"@

out-file -filepath c:\temp\test.xml -inputobject $xmlContent

when I run it, I get the message;

ERROR: Arg0 : The term ‘Arg0’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
ERROR: included, verify that the path is correct and try again.

so my question: how can I put that line into a here-string to be able to successfully create my file? What is the correct syntax?

thanks!

These are arguments to a function that you’re not showing us? I’m guessing that would be like:

$($args[0]) $($args[1]) $($args[2])

js is taking a guess, but what is $arg or $args? You need to be careful as ‘args’ is a Powershell keyword. Are the arguments you are inputting space delimited? Here are two methods, which using method 2 is better as you will get errors if the XML is malformed vs method 1 is just manipulating it as text:

$arguments = 'fee','fi','fo', 'fum'

#writing manaully as text
$xmlContent = @”
<?xml version="1.0" encoding="ISO-8859-1"?>
<settings>
    <arguments>$($arguments -join ' ')</arguments>
</settings>
"@

$xmlContent

#Writing as XML
[xml]$xmlContent = @"
<?xml version="1.0" encoding="ISO-8859-1"?>
<settings>
    <arguments></arguments>
</settings>
"@

$xmlContent.settings.arguments = $arguments -join ' '

$xmlContent.OuterXml

well, this was a facepalm moment.

i created my here-string with a single-quote instead of double-quote and it works as expected now. sorry to have bothered you.

$hereString = @’

some text…

<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>

more text

'@

Note: it’s funny though to see that you totally missed the point by automatically assuming i was passing arguments to a script when I only tried to write the entire here-string to a file. :wink:

Unfortunately, assumptions need to be made when only a portion of the code is provided. The error posted indicates the $arg0 variable is not defined and that variable definition was not provided in the post and your provided example has double qoutes. Glad you figured out the issue.

I don’t see how that could possibly work. Is that run through Azure or something?

works like a charm!

$xmlFileContent = @’
<?xml version=“1.0” encoding=“UTF-16”?>
<Task version=“1.6” xmlns=“http://schemas.microsoft.com/windows/2004/02/mit/task”>
<RegistrationInfo>
<Author>xxxxx</Author>
<Description>Register this computer if the computer is already joined to an Active Directory domain.</Description>
<URI>\Microsoft\Windows\Workplace Join\Automatic-Device-Join-VPN</URI>
<SecurityDescriptor>D:AI(A;;FA;;;NS)(A;;GA;;;SY)(A;ID;FA;;;BA)(A;ID;GRGX;;;AU)</SecurityDescriptor>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id=“LocalSystem”>
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context=“LocalSystem”>
<Exec>
<Command>%SystemRoot%\System32\dsregcmd.exe</Command>
<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>
</Exec>
</Actions>
</Task>
'@
Out-File -FilePath c:\temp\azurejoin.xml -InputObject $xmlFileContent