1

I'm trying to use PowerShell (version 5.1) classes for the first time. For some reason, I get the above error message. I'm searching for similar errors but I'm really not seeing any good examples or how to fix it.

This is what my code looks like:

Main powershell script, EndToEnd.ps1

using module .\Common_Parser_12.23.psm1

Function Get-MethodContents{
  [cmdletbinding()]
  Param ( [string]$codePath, [string]$methodNameToReturn, [string]$followingMethodName)
  Process
  {
   ...
  }
} #end function

...

#main code###############################
...
#using a Powershell class
Write-Host "class line in main here" -ForegroundColor DarkRed
$CommonParser = [Common_Parser]::new($alarmIdDef)
Write-Host "got past class line in main" -ForegroundColor DarkRed
Remove-Module  .\Common_Parser_12.23.psm1 

This is what the class looks like: Common_Parser_12.23.psm1:

#requires -Version 2
#Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -Recurse -ErrorAction SilentlyContinue )

#Dot source the files
Foreach ($import in @($Public + $Private)) {
    Try {
        . $import.fullname
    }
    Catch {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

Export-ModuleMember -Function $Public.Basename



class Common_Parser {
   [string]$alarmId


   

   Common_Parser([string]$alarm_id)
   {
         $this.alarmId = $alarm_id
   }

#Methods

#Method to look for return contents in code/content given, as string
#Note that $returnTo is where we parse to (but this text is not included), as an end string. $returnFrom is where we start getting data to return.
[string] GetFileContents([string]$codePath, [string]$returnFrom, [string]$returnTo)
{
   ...
} #end of method

} #class

I'm not sure if directory structure matters or not. Right now, this is sitting in a share drive, Investigations/EndToEnd (so they are in the same directory):

Common_Parser_12.23.psm1
EndToEndParser - 12.23 xml.ps1

This is the error:

An error occurred while creating the pipeline.
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RuntimeException

I referred to these links making my classes for the first time: module classes export class class

Michele
  • 3,617
  • 12
  • 47
  • 81
  • @SantiagoSquarzon in my EndToEndParser - 12.23 xml.ps1 file I already have using module .\Common_Parser_12.23.psm1. Adding an extra dot in front of it has a syntax error where there's a space between dots like you show. Without the space between dots still gives error when running: The specified module 'Investigations\Common_Parser_12.23.psm1' was not loaded because no valid module file was found in any module directory. + CategoryInfo : InvalidOperation: (Inves...rser_12.23.psm1:String) [], ParentContainsErrorRecordException – Michele Dec 23 '21 at 17:02
  • Woops didn't see that one, you can ignore my comment – Santiago Squarzon Dec 23 '21 at 17:06

1 Answers1

0

This is getting to the class now without error: EndToEndParser - 12.23 xml.ps1

changed it to: 

using module .\Common_Parser_12.23b.psm1
...
Write-Host "class line in main here" -ForegroundColor DarkRed
$CommonParser = [Common_Parser]::new($alarmIdDef)
$Messages = $CommonParser.ProcessOmAlarmXml()
Write-Host "Message from CommonParser: $Messages"
Write-Host "got past class line in main" -ForegroundColor DarkRed

Get-Module | Remove-Module  #.\Common_Parser_12.23.psm1 #this doesn't work

And for Common_Parser_12.23b.psm1:

changed it to:
$Public = @( Get-ChildItem -Path $PSScriptRoot\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\*.ps1 -Recurse -ErrorAction SilentlyContinue )

#Dot source the files
Foreach ($import in @($Public + $Private)) {
    Try {
        . $import.fullname
    }
    Catch {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

Export-ModuleMember -Function $Public.Basename
...

So the error message is gone. I verified file paths to the files were correct for references in the code, and that fixed it.

Michele
  • 3,617
  • 12
  • 47
  • 81