I have a Function (powershell 5.1) that is using regular expressions to get info out of 3 separate files/strings using the same regex. I want to process all 3 in the same Function and return it as an array of System.Collections(maybe Hashtable ...not sure if that's needed). But I'm getting the above error. It makes sense for me to do it this way because otherwise I will have separate functions doing the exact same thing, with the same regex. So I'm trying to re-use functionality to do the same regex on 3 different strings/files.
I looked up the error and it says something isn't assigned. not assigned I'm not seeing what isn't assigned in my case.
Function ProcessOmAlarmXml{
[cmdletbinding()]
Param ()
Process
{
$OMs = [System.Collections]::new() #this will store the 3 hashtables from the regex
$pathViewBase = 'C:\EndToEnd_view\'
$XML_OmMap_Dirs = @('\OmAlarmMap.xml') #will add the other 2 later
$XML_OmMap_Names = @('Config1','Config2','Config3')
$i = 0
#get each one from array
foreach($omFile in $XML_OmMap_Dirs)
{
$pathToXml = Join-Path -Path $pathViewBase -ChildPath $omFile
if(Test-Path $pathToXml)
{
#get file contents to parse as string
$fileContent = Get-MethodContents -codePath $pathToXml -methodNameToReturn "<Configuration>" -followingMethodName "</Configuration>"
#get the Mapping from omMap xml file
$errorMap = @{}
# create array to collect keys that are grouped together
$keys = @()
#regex works perfectly
switch -Regex ($fileContent -split '\r?\n') { #test regex with https://regex101.com/
'Name=\"(\w+-\w+)' { #12-5704
# add relevant key to key collection
$keys = $Matches[1] } #only match once
'^[\s]+<Value Name="MsgTag[">]+(\w+[.\w]*)<\/Value' { # gets the word after MsgTag, before closing tag (\w+)<\/Value MsgTag[">]+(\w+)<\/Value ([?:<!\s\S]?"MsgTag[">]+)(\w+[.\w]*)<\/Value #fails if line commented out..still captures line
# we've reached the relevant error, set it for all relevant keys
foreach($key in $keys){
#Write-Host "om key: $key"
$errorMap[$key] = $Matches[1]
Write-Host "om key: $key ... value: $($errorMap[$key])"
}
}
'break' {
# reset/clear key collection
$keys = @()
}
}#switch
#I'm trying to add each $errorMap with the name in the array $XML_OmMap_Names so I can pull them out and use them later meaningfully
[void]$OMs.Add({$XML_OmMap_Names[$i]=$errorMap}) #gives error message
Write-Host $OMs.Count
Write-Host $OMs -ForegroundColor Cyan
$i++
}#test-Path
else
{
Write-Host "No such path $pathToXml"
}
} #foreach
return $errorMap #will return $OMs later instead
} #end Process
}# End of Function
I'm also trying to store my objects in the array like this seems to be using: arrays.
Note that I'm trying to provide a minimal amount of info here, and want to show enough of what the function does so it makes sense of why I need to store the hashtables in an array to return without re-writing complex code which would still be pretty complex. It would be great if someone has more info on how to store the hashtables in the array to return all 3 in the array.
More info on the error:
You cannot call a method on a null-valued expression.
At C:\Users\2021\temp endToEnd folder while network down\EndToEndParser.ps1:329 char:11
+ [void]$OMs.Add({$XML_OmMap_Names[$i]=$errorMap}) #[System. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull