question

anupvtr avatar image
0 Votes"
anupvtr asked anupvtr edited

How to remove white space stored in $null in powershell script.

Thanks in Advance,

I am quite new to the PowerShell. Any help will be thankful.

Requirement:

1) if there is no value present in the Environment filed <Environment></Environment>. Need to print as "No Environment Present" .
2) if there is value present in the Environment filed <Environment>P01</Environment>. Need to print as " Environment Present" .

I am having a XMl file(File is attached). I need to print as "No Environment Present" if there is no value present in the Environment filed. I have wrote a small script as below. Even though the <Environment></Environment> filed doesn't contain any data. It will print as "Environment Present".

There is no valid data is present in the $Envmnts variable PowerShell will assume there is data is stored to $Envmnts due to the white space. Always the output will be "Environment Present". Please help me.

Script


[xml]$xmlKibana=get-content -path C:\Work_test\KibanaCode.xml
$Envmnts=$xmlKibana.Kibanadecode.KB.Environment
$Envmnts
if($Envmnts -eq $null)
{
Write-host "No Environment Exist"
}
else
{
Write-host "Environment Present"
}

===========================
Sample Xml file

<?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>PRD1</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>
======================================================================

161149-kibanacode.xml




windows-server-powershell
kibanacode.xml (1.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered anupvtr edited

Hi @anupvtr ,

maybe this is helpful. I used your Kibanacode.xml with 3 KB entries.

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

I just entered a value in your xml for the <environment> field for testing


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

Regards
Andreas Baumgarten

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AndreasBaumgarten,

Thank you very much. Really appreciate your help.

Could you please help me to complete my requirement. I tried in different ways but not getting succeeded. My doubt is created as another question in our forum.

https://docs.microsoft.com/en-us/answers/questions/678676/how-can-we-write-a-condition-for-missing-a-field-i.html


Regards,
Anu

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

The variable "$Envmnts" is, in the case of the supplied file, an array of three objects. Each of those objects is a simple string with a length of zero.

Your code "if($Envmnts -eq $null)" isn't checking the contents of the array, it's checking if the variable "$Envmnts" is empty (which it isn't).

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.