【发布时间】:2022-01-24 15:05:34
【问题描述】:
我第一次尝试使用 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 $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
我不确定目录结构是否重要。现在,它位于共享驱动器中,Investigations/EndToEnd(所以它们在同一个目录中):
Common_Parser_12.23.psm1
EndToEndParser - 12.23 xml.ps1
这是错误:
An error occurred while creating the pipeline.
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RuntimeException
我第一次参考了这些链接来制作我的课程: module classes export class class
【问题讨论】:
-
@SantiagoSquarzon 在我的 EndToEndParser - 12.23 xml.ps1 文件中我已经使用模块 .\Common_Parser_12.23.psm1。在它前面添加一个额外的点会出现语法错误,如您显示的点之间有空格。运行时没有点之间的空格仍然会出错:未加载指定的模块“Investigations\Common_Parser_12.23.psm1”,因为在任何模块目录中都找不到有效的模块文件。 + CategoryInfo : InvalidOperation: (Inves...rser_12.23.psm1:String) [], ParentContainsErrorRecordException
-
哎呀没看到那个,你可以忽略我的评论
标签: powershell class