Unable To Convert Sensitive Variable To String

Hi ,

I am using variable name called “LoginPassword” in project variable which I have marked as sensitive.
I am using powershell script step to update the confi.xml by referring #{LoginPassword} on place holder in below powershell script which is not working and throwing following error :

PowerShell Code

#Create Functions To Load & Save Target XML Config Files
    function LoadXML ($pathtoXML) {
        $script:xmlFile = "$pathtoXML"
        $script:xml = New-Object xml
        $xml.psbase.PreserveWhitespace = $true
        $xml.Load($xmlFile)
    }

    function SaveXML {

        $xml.Save($script:xmlFile)

    }
    $XMLPath = "C:\Support\Config.xml"
    LoadXML -pathtoXML "$XMLPath"
    $xpath = "/Configuration/Data/Section/Item[@Name='SeatInformation']"
    $node = $xml.SelectSingleNode($xpath)
    $node.Password = #{LoginPassword}
	Write-Host $LoginPassword

Note :

  1. Variable Substitution is not possible in not recommend as because of it’s dynamic variable and password keeps changing
  1. Need a logic which can convert the secure variable to string automatically and insert string in config.xml or any other configuration file.

Thanks
Vivek
.

Hi Vivek,

Thank you for reaching out with your question about sensitive variables.

It looks like the syntax of the line where you assign the password isn’t quite right. I believe it should be:

$node.Password = "${LoginPassword}"

Could you please try this and let me know how you get on?

Best Regards,

Charles

Hi Charles,

Thanks for responding. Unfortunately, it is not working.

After Modification PowerShell Code

#Create Functions To Load & Save Target XML Config Files

    function LoadXML ($pathtoXML) {
        $script:xmlFile = "$pathtoXML"
        $script:xml = New-Object xml
        $xml.psbase.PreserveWhitespace = $true
        $xml.Load($xmlFile)
    }

    function SaveXML {

        $xml.Save($script:xmlFile)

    }
    $XMLPath = "C:\Support\Config.xml"
    LoadXML -pathtoXML "$XMLPath"
    $xpath = "/Configuration/Data/Section/Item[@Name='SeatInformation']"
    $node = $xml.SelectSingleNode($xpath)
    $node.Password = "${LoginPassword}"
    $LoginPassword = $node.Password
    Write-Host $LoginPassword

Hi Vivek,

Thank you for getting back to me so quickly.

It looks like the “LoginPassword” variable name is being used twice - there’s the one from within Octopus Deploy and there is one created from $node.Password. I’d recommend changing the second one to have a different name.

It may be worth noting that the “Write-Host $LoginPassword” line should only display “******” in the task logs, as Octopus Deploy will attempt to censor sensitive variables.

Could you please try the change above and advise what error message you see (if any)?

Best Regards,

Charles

Hi,

Actually, you were right at your suggestion. It’s working fine and the only culprit is that I am not saving the xml after writing the password to file.

Thanks for the Support

Final PowerShell Code

#Create Functions To Load & Save Target XML Config Files
    function LoadXML ($pathtoXML) {
        $script:xmlFile = "$pathtoXML"
        $script:xml = New-Object xml
        $xml.psbase.PreserveWhitespace = $true
        $xml.Load($xmlFile)
    }

    function SaveXML {

        $xml.Save($script:xmlFile)

    }
    $XMLPath = "C:\Support\Config.xml"
    LoadXML -pathtoXML "$XMLPath"
    $xpath = "/Configuration/Data/Section/Item[@Name='SeatInformation']"
    $node = $xml.SelectSingleNode($xpath)
    $node.Password = "${LoginPassword}"
	SaveXML

Hi Vivek,

Thank you for letting me know, I’m really pleased that it’s working now!

Please get back in touch with us if you have any questions.

Best Regards,

Charles

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.