2

Is it possible to insert a variable from PowerShell into an XML format? ex. I have declared $SIDadm = sidadm as a global variable and i want it to be inserted on xml code below

[xml]$REST2 = 
'<config xmlns="http://www.sap.com/lmsl/slp">
<Parameter>
<id>JeeAdminPassword</id>
<value>mkSVQW1zMi070N</value>
</Parameter>
<Parameter>
<id>SidAdmUserPassword</id>
<value>cB8y@OSLHu@DcNPa</value>
</Parameter>
<Parameter>
<id>NoCredentialsMode</id>
<value>false</value>
</Parameter>
<Parameter>
<id>SidAdmUserName</id>
<value>$SIDadm</value>
</Parameter>
<Parameter>
<id>EHPStackFile</id>
<value>F:/SAPSoftware/J3M_MII15.1_SP3_Patch_1/Stack_generic.xml</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[]</id>
<value>2</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].SidAdmUserPassword</id>
<value>cB8y@OSLHu@DcNPa</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].InstanceNumber</id>
<value>0</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].SidAdmUserName</id>
<value>GLOBEDDC\j3ladm</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].HostName</id>
<value>j3lsap</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].SidAdmUserPassword</id>
<value>cB8y@OSLHu@DcNPa</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].HostName</id>
<value>j3lsap</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].InstanceNumber</id>
<value>2</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].SidAdmUserName</id>
<value>GLOBEDDC\j3ladm</value>
</Parameter>
<Parameter>
<id>InstanceName</id>
<value>JC00</value>
</Parameter>
<Parameter>
<id>SystemId</id>
<value>J3L</value>
</Parameter>
<Parameter>
<id>JeeAdminUser</id>
<value>Administrator</value>
</Parameter>
</config'

return $REST2

Basically I want to insert $SIDadmwhich has a value of sidadm into the XML. like this under the (id)SidAdmUserName(/id) its (value) would be (value)sidadm(/value)

[xml]$REST2 = 
'<config xmlns="http://www.sap.com/lmsl/slp">
<Parameter>
<id>JeeAdminPassword</id>
<value>mkSVQW1zMi070N</value>
</Parameter>
<Parameter>
<id>SidAdmUserPassword</id>
<value>cB8y@OSLHu@DcNPa</value>
</Parameter>
<Parameter>
<id>NoCredentialsMode</id>
<value>false</value>
</Parameter>
<Parameter>
<id>SidAdmUserName</id>
<value>sidadm</value>
</Parameter>
<Parameter>
<id>EHPStackFile</id>
<value>F:/SAPSoftware/J3M_MII15.1_SP3_Patch_1/Stack_generic.xml</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[]</id>
<value>2</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].SidAdmUserPassword</id>
<value>cB8y@OSLHu@DcNPa</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].InstanceNumber</id>
<value>0</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].SidAdmUserName</id>
<value>GLOBEDDC\j3ladm</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[0].HostName</id>
<value>j3lsap</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].SidAdmUserPassword</id>
<value>cB8y@OSLHu@DcNPa</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].HostName</id>
<value>j3lsap</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].InstanceNumber</id>
<value>2</value>
</Parameter>
<Parameter>
<id>SapSystemInstance[1].SidAdmUserName</id>
<value>GLOBEDDC\j3ladm</value>
</Parameter>
<Parameter>
<id>InstanceName</id>
<value>JC00</value>
</Parameter>
<Parameter>
<id>SystemId</id>
<value>J3L</value>
</Parameter>
<Parameter>
<id>JeeAdminUser</id>
<value>Administrator</value>
</Parameter>
</config'

$nsm = New-Object Xml.XmlNamespaceManager($REST2.NameTable)
$nsm.AddNamespace('ns', $REST2.DocumentElement.NamespaceURI)

$xpath = '/ns:config/ns:parameter[ns:id/text()="SidAdmUserName"]/ns:value'

$REST2.SelectSingleNode($xpath, $nsm).'#text' = $SIDadm
$REST2.Save([Console]::Out)

return $REST2
2
  • It's not clear to me what you're asking here. Do you have XML with variables in it and want to expand the variables to their value? Insert the variables into the XML (template)? Insert their values into the XML (template)? Just a specific variable or all of them? Something else entirely? Commented Jan 5, 2017 at 12:16
  • i want to use the $SIDadm variable from powershell inside the xml format sorry for being unclear first time posting here @AnsgarWiechers Commented Jan 5, 2017 at 12:25

1 Answer 1

4

To expand variables in an XML string you can use the automatic variable $ExecutionContext:

$str = @'
<config xmlns="http://www.sap.com/lmsl/slp">
  <parameter>
    <id>SidAdmUserName</id>
    <value>$SIDadm</value>
  </parameter>
</config>
'@

$ExecutionContext.InvokeCommand.ExpandString($str)

Beware that this will expand all expandable expressions in the string (e.g. something like $(Get-ChildItem) would be expanded as well).

If you want to expand just a variable in a single element you could try with a simple string replacement:

$str = @'
<config xmlns="http://www.sap.com/lmsl/slp">
  <parameter>
    <id>SidAdmUserName</id>
    <value>$SIDadm</value>
  </parameter>
</config>
'@

$str.Replace('$SIDadm', $SIDadm)

However, that may also yield undesired results, e.g. if there are several variables starting with the same partial name. Something like

<config xmlns="http://www.sap.com/lmsl/slp">
  <parameter>
    <id>SidAdmUserName</id>
    <value>$SIDadm</value>
  </parameter>
  <parameter>
    <id>SidAdmPassword</id>
    <value>$SIDadmPassword</value>
  </parameter>
</config>

would become

<config xmlns="http://www.sap.com/lmsl/slp">
  <parameter>
    <id>SidAdmUserName</id>
    <value>j3ladm</value>
  </parameter>
  <parameter>
    <id>SidAdmPassword</id>
    <value>j3ladmPassword</value>
  </parameter>
</config>

In situations like that it's better to parse the XML, select the particular node, and replace the content of just that node:

[xml]$xml = @'
<config xmlns="http://www.sap.com/lmsl/slp">
  <parameter>
    <id>SidAdmUserName</id>
    <value>$SIDadm</value>
  </parameter>
  <parameter>
    <id>SidAdmPassword</id>
    <value>$SIDadmPassword</value>
  </parameter>
</config>
'@

$nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
$nsm.AddNamespace('ns', $xml.DocumentElement.NamespaceURI)

$xpath = '/ns:config/ns:parameter[ns:id/text()="SidAdmUserName"]/ns:value'

$xml.SelectSingleNode($xpath, $nsm).'#text' = $SIDadm
$xml.Save([Console]::Out)

Note that you MUST use a namespace manager here, because your XML uses namespaces.

Sign up to request clarification or add additional context in comments.

20 Comments

@Therealjubster Please do not accept an answer before you have verified that the proposed solution actually does what you expect it to do.
hello @AnsgarWiechers im getting an error it says invocation failed because [System.Xml.XmlDocument] doesn't contain a method named 'Replace'. when insert the .replace method
@Therealjubster That's because you mixed my examples. Don't do that. The first two examples use XML strings, whereas the last example uses an XmlDocument object. The latter does not have a Replace() method.
hmm so is there anyway for my variable $SIDadm to be inserted into a XML template?
@Therealjubster I showed you 3 different ways in my answer, didn't I? Why don't you update your question with a sample of what your XML template looks like now, and what you want it to look like after the intended modification?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.