here-string with $($var) text

Welcome Forums General PowerShell Q&A here-string with $($var) text

Viewing 6 reply threads
  • Author
    Posts
    • #244955
      Participant
      Topics: 26
      Replies: 41
      Points: 381
      Rank: Contributor

      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!

    • #244961
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,690
      Helping Hand
      Rank: Community Hero

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

      • This reply was modified 2 months ago by js.
      • This reply was modified 2 months ago by js.
    • #244973
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      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:

    • #244982
      Participant
      Topics: 26
      Replies: 41
      Points: 381
      Rank: Contributor

      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. 😉

      • This reply was modified 2 months ago by Kardock.
    • #245030
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      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.

    • #245084
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,690
      Helping Hand
      Rank: Community Hero

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

    • #245634
      Participant
      Topics: 26
      Replies: 41
      Points: 381
      Rank: Contributor

      works like a charm!

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

       

Viewing 6 reply threads
  • You must be logged in to reply to this topic.