I haven't seen your full code so this is the best I can do.
Let's assume that you have the XML stored in variable $XML and $XML was defined like this:
[xml]$xml = @"
<Environments>
<Environment Database="exampleDatabase">
<Backup Restore="TRUE">
<DatabaseBackups Instance="SQLInstance" SrcLocation="\\Hostname\c$\temp\example.bak" DstLocation="\\Hostname\c$\temp\example.bak" DBOwner=""sa""/>
<DatabaseBackups Instance="SQLInstance" SrcLocation="\\Hostname\c$\temp\example2.bak" DstLocation="\\Hostname\c$\temp\example2.bak"/>
</Backup>
</Environment>
</Environments>
"@
You can check whether DBOwner exists or not just by checking the value with
$XML.Environments.Environment.Backup.DatabaseBackups.DBOwner
Which with the XML would output:
"sa"
But to get a true/false value on whether it exists or not, you can use the [bool] datatype like you did in your answer:
[bool]$XML.Environments.Environment.Backup.DatabaseBackups.DBOwner
But I found a glitch with this in which it would still output true if the node didn't exist like so:
[bool]$XML.Environments.Environment.Backup.DatabaseBackups.fasdasd
True
so I changed it to this.
[bool][string]$($XML.Environments.Environment.Backup.DatabaseBackups.DBOwner).trim
Which in this case would output:
True
And to put this in an if statement since it is already a boolean you could put it in the in statement like so:
if([bool][string]$($XML.Environments.Environment.Backup.DatabaseBackups.DBOwner).trim){
Invoke-Sqlcmd -ServerInstance "$($DatabaseBackup.Instance)" -Query "EXEC sp_changedbowner $($Environment.Backup.DatabaseBackup.DBOwner)"
} else {
Write-Output 'No DBOwner Specified'
}
To test if this works we can also try doing this on something that doesn't exist like $XML.Environments.Environment.Backup.DatabaseBackups.SomethingThatDoesntExist which should return false and does like so:
[bool][string]($XML.Environments.Environment.Backup.DatabaseBackups.SomethingThatDoesntExist).trim()
False
To test the if statement we can do
if([bool][string]$($XML.Environments.Environment.Backup.DatabaseBackups.DBOwner).trim){
Write-Output "Exists"
} else {
Write-Warning 'No DBOwner Specified'
}
Which would output
Exists
and we could also test if it doesn't exist with
if([bool][string]$($XML.Environments.Environment.Backup.DatabaseBackups.fasdasd).trim){
Write-Output "Exists"
} else {
Write-Warning 'No DBOwner Specified'
}
The output would be:
WARNING: No DBOwner Specified
NOTE:
The [bool]s in the if statements are unnecessary since if already converts things to booleans but I just put it in for easier understanding this would work fine too:
if([string]$($XML.Environments.Environment.Backup.DatabaseBackups.fasdasd).trim){
Write-Output "Exists"
} else {
Write-Warning 'No DBOwner Specified'
}