我创建了一个 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
有任何想法吗 ?非常感谢任何帮助。我真的很困惑。