Under to pass variable into <!CDATA[[ ]]>

i am not able to pass variable inside powershell <!CATA[]>

code is as below

#this code below will escapte SSL error for powershell
$code= @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
        public class TrustAllCertsPolicy : ICertificatePolicy {
            public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) {
                return true;
            }
        }
"@
Add-Type -TypeDefinition $code -Language CSharp
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#create function to convert xml to json

function ConvertFrom-Xml {
  param([parameter(Mandatory, ValueFromPipeline)] [System.Xml.XmlNode] $node)
  process {
    if ($node.DocumentElement) { $node = $node.DocumentElement }
    $oht = [ordered] @{}
    $name = $node.Name
    if ($node.FirstChild -is [system.xml.xmltext]) {
      $oht.$name = $node.FirstChild.InnerText
    } else {
      $oht.$name = New-Object System.Collections.ArrayList 
      foreach ($child in $node.ChildNodes) {
        $null = $oht.$name.Add((ConvertFrom-Xml $child))
      }
    }
    $oht
  }
}

#$dir='<![CDATA[c:\temp\12-aug-22]]>'

$path="c:\temp\12-aug-22"

$dir='<![CDATA[${path}]]>'
$dir
1 Like

Hi, welcome to the forum :wave:

$dir='<![CDATA[${path}]]>'

You have single quotes which declares a literal string. Use double quotes " for variable expansion.

$dir= "<![CDATA[${path}]]>"
3 Likes

Thanks for the quick help. It worked.

1 Like