我第一次尝试使用PowerShell(5.1版(类。由于某种原因,我收到了上面的错误消息。我正在搜索类似的错误,但我真的没有看到任何好的例子或如何修复它
这就是我的代码:
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
这就是类的样子:Common_Parser_12.23.psm1:
#requires -Version 2
#Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRootPublic*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRootPrivate*.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
我不确定目录结构是否重要。现在,它位于共享驱动器Investigations/EndToEnd中(所以它们在同一目录中(:
Common_Parser_12.23.psm1
EndToEndParser - 12.23 xml.ps1
这就是错误:
An error occurred while creating the pipeline.
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RuntimeException
我第一次在课堂上提到了这些链接:单元类导出类类
现在进入类时没有出现错误: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
对于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
...
因此,错误信息消失了。我验证了文件的文件路径在代码中的引用是正确的,并修复了它