【问题标题】:PowerShell class use - an error occurred while creating the pipelinePowerShell 类使用 - 创建管道时出错
【发布时间】: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


【解决方案1】:

现在进入课堂没有错误: 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
...

所以错误信息消失了。我验证了文件的文件路径对于代码中的引用是正确的,并且修复了它。

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2023-03-17
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多