In PowerShell I am trying to parse out data (either in quotes or between “> <“) in records found between every instance of tag1 (e.g. Like “AuditRecord AuditRecordID”) and tag2 (e.g. Like “/AuditRecord). The script to iterate with every file found in the directory that contains instances of said groupings.
I think I’m pretty close to achieving what I want but the code errors with “Unable to find type [regexoptions]”. I’m pretty new to this and this is the first time running into it. Not sure how to resolve this.
The Error
Unable to find type [regexoptions].
At C:atmosfind_data_please.ps1:27 char:59
+ ... s = [regex]::Matches($content, $recordPattern, [regexoptions]::Single ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (regexoptions:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
The Code
# Define the directory path
$directoryPath = "c:atmostest"
# Get all XML files in the directory
$files = Get-ChildItem -Path $directoryPath
# Function to extract data between tags
function Extract-DataBetweenTags {
param (
[string]$content,
[string]$startTag,
[string]$endTag
)
$pattern = [regex]::Escape($startTag) + "(.*?)" + [regex]::Escape($endTag)
$matches = [regex]::Matches($content, $pattern)
return $matches | ForEach-Object { $_.Groups[1].Value.Trim() }
}
# Process each file
foreach ($file in $files) {
$content = Get-Content -Path $file.FullName -Raw
# Extract records between <AuditRecord> and </AuditRecord>
$recordPattern = "<AuditRecord*?>(.*?)</AuditRecord>"
$records = [regex]::Matches($content, $recordPattern, [regexoptions]::Singleline)
$results = @()
foreach ($record in $records) {
$recordContent = $record.Groups[1].Value
# Extract data
$quotedStrings = [regex]::Matches($recordContent, '"(.*?)"') | ForEach-Object { $_.Groups[1].Value }
$beforeDateTime = Extract-DataBetweenTags -content $recordContent -startTag '<Before Type="xs:dateTime">' -endTag '</Before>'
$afterDateTime = Extract-DataBetweenTags -content $recordContent -startTag '<After Type="xs:dateTime">' -endTag '</After>'
$beforeLong = Extract-DataBetweenTags -content $recordContent -startTag '<Before Type="xs:long">' -endTag '</Before>'
$afterLong = Extract-DataBetweenTags -content $recordContent -startTag '<After Type="xs:long">' -endTag '</After>'
$beforeDouble = Extract-DataBetweenTags -content $recordContent -startTag '<Before Type="xs:double">' -endTag '</Before>'
$afterDouble = Extract-DataBetweenTags -content $recordContent -startTag '<After Type="xs:double">' -endTag '</After>'
# Create a result object
$result = [PSCustomObject]@{
QuotedStrings = $quotedStrings -join ", "
BeforeDateTime = $beforeDateTime -join ", "
AfterDateTime = $afterDateTime -join ", "
BeforeLong = $beforeLong -join ", "
AfterLong = $afterLong -join ", "
BeforeDouble = $beforeDouble -join ", "
AfterDouble = $afterDouble -join ", "
}
$results += $result
}
# Write results to CSV
$csvFileName = [System.IO.Path]::Combine($directoryPath, [System.IO.Path]::GetFileNameWithoutExtension($file.Name) + "_processed.csv")
$results | Export-Csv -Path $csvFileName -NoTypeInformation
}
Sample Block
<AuditRecord AuditRecordID="ca7a433e-b45f-4ed1-8169-5fc70547b35e">
<PIUser UserID="1" Name="master_admin"/>
<PITime UTCSeconds="1708987340" LocalDate="2024-02-26T17:42:20-05:00" />
<PIConfigurationDB>
<PIModules Action="Edit">
<PIModule UniqueID="7c03e74e-ad46-4ada-8234-50c7cd2699f0, 31-Dec-69 16:00:01" Name="MDB-AFMigrationData">
<PIModuleAttributes>
<PIModuleAttribute Name="ModifyDate">
<Value>
<Before Type="xs:dateTime">2024-02-26T17:42:05-05:00</Before>
<After Type="xs:dateTime">2024-02-26T17:42:20-05:00</After>
</Value>
</PIModuleAttribute>
<PIModuleAttribute Name="Revision">
<Value>
<Before Type="xs:long">222325</Before>
<After Type="xs:long">222326</After>
</Value>
</PIModuleAttribute>
</PIModuleAttributes>
<PIProperties>
<PIProperty Name="AFCST" Action="Edit" ParentUNC_Name="\PIProperties">
<Value>
<Before Type="xs:double">1708987319.220131</Before>
<After Type="xs:double">1708987325.796927</After>
</Value>
</PIProperty>
</PIProperties>
</PIModule>
</PIModules>
</PIConfigurationDB>
</AuditRecord>
Any guidance is greatly appreciated. I prefer the regex approach as the .xml path would yielded nothing but null .csv’s regardless of any attempt.