How can we write a condition for missing a field in XML file using powershell.

ANU 336 Reputation points
2021-12-29T20:40:14.54+00:00

Thanks in advance.

I need to write a powershell script to check whether the KB is present in the attached XML file. I am quite new to the PowerShell world. But i tried in my own ways unfortunately not getting succeed. The script is checking few conditions after and before this requirement. I hope the rest of the script is working fine with the help of few good peoples. Can any one help me on this

Requirement:

  1. Check the KB portion is present in the file
  2. If Kb portion is not present write- host " KB not present"
  3. If kb portion is present continue the script

Sample Script.

=============================================

$xmlpath = "C:\Work_test\KibanaCode.xml"
$p=Test-path $xmlpath
switch($true)
{
(!$p)
{
write-host"File Not Exist"
}
($p)

xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{
[xml]$xmlKibana=get-content -path C:\Work_test\KibanaCode.xml
$xmlKibana.Kibanadecode.KB | ForEach-Object {
if ($_.Environment) { $status = "Present" }
else { $Status = "not present" }
}
$Status
}
}

===============================

Sample XML with out KB

================

<?xml version="1.0" encoding="UTF-8"?>
<Kibanadecode>
<FileDetails>Kibana Code Output</FileDetails>
</Kibanadecode>

=================================

Sample XML with KB

=============================

<?xml version="1.0" encoding="UTF-8"?>
<Kibanadecode>
<FileDetails>Kibana Code Output</FileDetails>
<KB>
<ID>KB/135</ID>
<KBMachine>HTZAPP10</KBMachine>
<KBMainMachine>HAPMAIN01</KBMainMachine>
<Environment></Environment>
<Build>Windows</Build>
<StartTime>21/08/2021 09:15:01</StartTime>
<EndTime>21/08/2021 10:35:13</EndTime>
<Output>Success</Output>
<Details>
</Details>
<steps>
<step-1>
<stepname>Fis Push</stepname>
<stepcommand>Oracle.exe</stepcommand>
<Output>Success</Output>
</step-1>
<step-2>
<stepname>Block chain</stepname>
<stepcommand>Oracle.exe</stepcommand>
<Output>Success</Output>
</step-2>
</steps>
</KB>
<KB>
<ID>KB/135</ID>
<KBMachine>HTZAPP10</KBMachine>
<KBMainMachine>HAPMAIN01</KBMainMachine>
<Environment></Environment>
<Build>Windows</Build>
<StartTime>21/08/2021 09:15:01</StartTime>
<EndTime>21/08/2021 10:35:13</EndTime>
<Output>Success</Output>
<Details>
</Details>
<steps>
<step-1>
<stepname>Fis Push</stepname>
<stepcommand>Oracle.exe</stepcommand>
<Output>Success</Output>
</step-1>
<step-2>
<stepname>Block chain</stepname>
<stepcommand>Oracle.exe</stepcommand>
<Output>Success</Output>
</step-2>
</steps>
</KB>
</Kibanadecode>

===========================================================

161176-kibanacode.xml

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.6K Reputation points MVP Volunteer Moderator
    2021-12-30T10:29:15.96+00:00

    Hi @ANU ,

    to be honest ... it doesn't make a big difference in the script logic if check for "exists" before ""not exists" or vice versa. The result will be the same.

    But anyway. You just have to modify the "if" conditions + and the the "else" conditions.

    Get-Item -Path "C:\Junk\kibanacode*.xml" | ForEach-Object {  
        [xml]$xmlContent = Get-Content -Path $_  
        if (!$xmlContent.Kibanadecode.KB) {  
            Write-Output "$_ - KB not present"  
        }  
        else {  
            Write-Output "$_  - KB present"  
            $xmlContent.Kibanadecode.KB | ForEach-Object {  
                if (!$_.Environment) { Write-Output "  Environment not present" }  
                else { Write-Output "  Environment present" }  
            }  
        }  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.6K Reputation points MVP Volunteer Moderator
    2021-12-29T20:56:06.583+00:00

    Hi @ANU ,

    here we go:

    $xmlFile = "C:\Junk\kibanacode.xml"  
    [xml]$xmlContent = Get-Content -Path $xmlFile  
    if ($xmlContent.Kibanadecode.KB) {Write-Output "KB present"}  
    else {Write-Output "KB not present"}  
    

    If you have more than one xml file in the folder this might help to check all files in folder:

    Get-Item -Path "C:\Junk\kibanacode*.xml" | ForEach-Object {  
        [xml]$xmlContent = Get-Content -Path $_  
        if ($xmlContent.Kibanadecode.KB) {  
            Write-Output "$_ KB present"  
        }  
        else {  
            Write-Output "$_ KB not present"  
        }  
    }  
    

    Output will look like this:

    161196-image.png

    And combined with the other part of the script (checking the environment field: https://learn.microsoft.com/en-us/answers/questions/678642/how-to-remove-white-space-stored-in-null-in-powers.html?childToView=678618#comment-678618):

    Get-Item -Path "C:\Junk\kibanacode*.xml" | ForEach-Object {  
        [xml]$xmlContent = Get-Content -Path $_  
        if ($xmlContent.Kibanadecode.KB) {  
            Write-Output "$_  - KB present"  
            $xmlContent.Kibanadecode.KB | ForEach-Object {  
                if ($_.Environment) { Write-Output "  Environment Present" }  
                else { Write-Output "  Environment not present" }  
            }  
        }  
        else {  
            Write-Output "$_ - KB not present"  
        }  
    }  
    

    Output looks like this:

    161208-image.png

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. ANU 336 Reputation points
    2021-12-30T09:18:01.477+00:00

    Hi @Andreas Baumgarten .

    Thanks for the help. Is it possible can we check the not condition of KB first and proceed the Script workflow.

    Script work flow.

    1) If KB section is not present then print " KB is not Present" No need for KB healthy condition check . Then proceed to Environment section for Not present condition first.

    Thanks
    Nikhil GS


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.