IF XML Node = Variable

Hi,

Hope someone can help, I’m tying to create an IF statement to say:

[pre]IF ($c -eq $nPrice)
{
‘Yes’
} Else {
‘No’
}[/pre]

$c and $nPrice are both the same value but the result is always No, i suspect its because they are 2 different data types?

$c is a token from a csv file

$nPrice is a node (if that’s the correct term) from an XML response

Can you help me understand what i am doing wrong please.

Regards,

Jamie

Importing from CSV or XML (not Import-CliXml) would both be strings. Powershell will attempt to do conversions to numeric values if it needs to for math or comparisons without having to cast. You can use .GetType() to determine what type those variable are:

'$c: {0}' -f $c
$c.GetType()

'$nPrice: [0}' -f $nPrice
$nPrice.GetType()

IF ($c -eq $nPrice)
{
'Yes'
} Else {
'No'
}

Without seeing the variable values, examples (xml, csv), it’s going to be difficult to help you troubleshoot further

Hi,

Thank you for your reply, sorry i meant to add the values to the first post, they are currency values but without £ such as 160.49 and 160.28

The csv file is made up of 3 columns separated by a pipe:

Part 1|“Part 1 Description”|160.49
Part 2|“Part 2 Description”|160.28

I am trying to check if $c (the price) matches nPrice (The new price from the XML API)

[pre]# Loop through part numbers and prices
Get-Content “UNCPath\fileName.csv” | ForEach {
$a,$b,$c = $_.Split("|")

Create XML Request

$convID = New-Guid
[xml]$xreq = get-content “UNCPath\Template.xml”
$xreq.message.header.conversationID = $convID.ToString()
$xreq.message.body.poAvailabilityRequest.partNo = $a

Send XML request and get response

$response = Invoke-RestMethod -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)} ‘URL.com/API’ -Method Post -Body $xreq -ContentType ‘text/xml’
$nPrice = $response.SelectSingleNode("//salesPrice")

IF ($c -eq $nPrice)
{
‘Yes’
} Else {
‘No’
}
}[/pre]

If you check $nPrice, it’s probably still an object like #text. Using the xml in this blog, if we get the first node for AddressPrefix:

PS C:\WINDOWS\system32> $xml.SelectSingleNode('//AddressPrefix')

#text         
-----         
192.168.1.0/24


PS C:\WINDOWS\system32> $xml.SelectSingleNode('//AddressPrefix').InnerText
192.168.1.0/24

Another method is using dot notation to get to the string value:

PS C:\WINDOWS\system32> $xml.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite.AddressSpace.AddressPrefix

192.168.1.0/24

Thank you Rob, dot notation worked perfect, I can’t believe i didn’t try that as i was using dot notation before but removed it when troubleshooting an earlier error that i thought was attributed to that.

Thank you for your support.