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