0

我创建了一个 PSObject,它有几个属性和几个方法。唯一的问题是将参数传递给方法。

创建对象、其属性和方法后,我可以使用 get-members 方法查看成员。Equals 方法有一个参数,我的方法没有。

function get_ExcelObj {
         param ([string] $SheetName, [string] $AppDir, [string] $NameApp )

         [string] $SheetName  = "DB"
         [string] $BaseDir    = ""
         [object] $ExcelHash  = @{}  
         [object] $Index      = @{}

         $EE_DB = New-Object -TypeName PSObject

         Add-Member -InputObject $EE_DB -MemberType NoteProperty -Name ExcelDB        -Value $ExcelHash
         Add-Member -InputObject $EE_DB -MemberType NoteProperty -Name IndexDB        -Value $Index
         Add-Member -InputObject $EE_DB -MemberType NoteProperty -Name SheetName      -Value $SheetName
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name InitExcel      -Value $InitExcel
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name GetSheetName   -Value $GetSheetName
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name OpenExcelFile  -Value $OpenExcelFile
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name OpenExcelsheet -Value $OpenExcelsheet

         return  $EE_DB

}
$OpenExcelFile = { 
     param ( [string] $ExcelFile )
     $WorkBook = $this.ObjExcel.Workbooks.Open($ExcelFile)
     Add-Member -InputObject $this -MemberType NoteProperty -Name WorkBook  -Value $WorkBook 
}
$ExcelObj = get_ExcelObj -SheetName "DB" -AppDir $MedGovDir -NameApp "MedGov"
$ExcelObj | Get-Member # Lots of members, eg: Method bool Equals(System.Object obj)  
$ExcelObj.OpenExcelFile() -ExcelFile $file # does not work

有任何想法吗 ?非常感谢任何帮助。我真的很困惑。

4

1 回答 1

1

在 .NET 对象上调用方法时,您可以像在 C# 或 VB.NET 中那样传递参数,逗号分隔的顺序是参数出现在方法签名中的顺序,在括号内。尝试以下操作:

$ExcelObj = get_ExcelObj -SheetName "DB" -AppDir $MedGovDir -NameApp "MedGov"
$ExcelObj | Get-Member # Lots of members, eg: Method bool Equals(System.Object obj)
$ExcelObj.OpenExcelFile($file) # this should work
$ExcelObj.OpenExcelFile() -ExcelFile $file # does not work
于 2019-05-03T16:16:42.397 回答