Powershell Regex MDT xml

Hi,

I need to get the “name”, “description” and “disable” from a file by using regex.

file content extract :

<step type=“BDD_RunPowerShellAction” name=“1.0 ---------- DEBUG JRAM ----------” description=“” disable=“false” continueOnError=“false” successCodeList=“0 3010”>

<step type=“BDD_Gather” name=“Gather local only” disable=“false” continueOnError=“false” successCodeList=“0 3010” description=“” startIn=“”>

As you can see the order is different on the two lines (on the first one, there is “step type” then “name” then "description and finally “disable”, this is all I need, and on the second it is “step type” then “name” then “disable” and “description” is at the end).

I have done this but the regex is not good :

if ($_ -match ““false””) { $StepNum++ $Step = $_ $Step -match “name=(?.*) disable=” $Name = $matches[‘content’] Write-Host " Step Name ($StepNum) : $Name" -ForegroundColor Yellow " Step Name ($StepNum) : $Name" | Out-File -FilePath “$FileDot” -Append } }

Thank you very much for your help.

Jo

Jo, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

If your file is a proper XML file you don’t have to use - actually you should not use - regex to get nodes, attributes, names, properties or information in general from an XML file.

Here you can find helpful information about working with XML and Powershell: Mastering everyday XML tasks in PowerShell.

If you like to post XML content you should use an external service like Github Gist because the forum software here does not handle xml content gracefully.

Treating it like xml seems easier.

[xml]$xml = @'
<top>
  <step type="BDD_RunPowerShellAction" name="1.0 ———- DEBUG JRAM ———-"
    description="" disable="false" continueOnError="false"
    successCodeList="0 3010" />
  <step type="BDD_Gather" name="Gather local only" disable="false"
    continueOnError="false" successCodeList="0 3010"
    description="" startIn="" />
</top>
'@

$xml.top.step | select name,description,disable

name                     description disable
----                     ----------- -------
1.0 ———- DEBUG JRAM ———-             false
Gather local only                    false

Nice unicode quotation marks (U+201C, LEFT DOUBLE QUOTATION MARK, and U+201D, RIGHT DOUBLE QUOTATION MARK).

'"name"' | format-hex -Encoding BigEndianUnicode


           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   20 1C 00 6E 00 61 00 6D 00 65 20 1D               ..n.a.m.e .