0

The context is that I'm working on a set of wrappers for a PowerShell flow (interpreter, if you will) to add simplicity and - above all - standardize reports being made by my colleagues.

The snippet I made to experiment with this is as following:

Add-Type -Path ".\itextsharp.dll"

function CreateReport([string]$Path = ".\Report.pdf")
{
    #Create Document, define basic properties
    $Document = New-Object iTextSharp.text.Document
    $Document.SetPageSize([iTextSharp.text.PageSize]::A4)
    $Document.SetMargins(5,5,5,5)
    $Document.AddAuthor($env:USERNAME)

    #Define methods
    Add-Member -in $Document scriptmethod AddParagraph {
        param([string]$Text = "Empty paragraph")
        $Paragraph = New-Object iTextSharp.text.Paragraph
        $Paragraph.Add($Text)
        $this.Add($Paragraph)
    }
    return $Document
}

(I am well aware that parameter $Path doesn't do anything, this is for later use)

To my understanding this should work, yet it doesn't. Worst of all, I can't figure out why. I test it as follows:

$TestDoc = CreateReport
$TestDoc.AddParagraph("asdf")

And it throws me the following exception:

Method invocation failed because [System.Boolean] does not contain a method named 'AddParagraph'.
At line:1 char:1
+ $TestDoc.AddParagraph("asdf")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Why does PowerShell think I'm trying to call a method in a boolean? Doesn't it recognize/support the iTextSharp.text.Document object type?


As asked, here's the list of members from CreateReport, my function:

PS C:\temp\Robin> CreateReport | Get-Member


   TypeName: System.Boolean

Name        MemberType Definition                                                                                                                                        
----        ---------- ----------                                                                                                                                        
CompareTo   Method     int CompareTo(System.Object obj), int CompareTo(bool value), int IComparable.CompareTo(System.Object obj), int IComparable[bool].CompareTo(bool...
Equals      Method     bool Equals(System.Object obj), bool Equals(bool obj), bool IEquatable[bool].Equals(bool other)                                                   
GetHashCode Method     int GetHashCode()                                                                                                                                 
GetType     Method     type GetType()                                                                                                                                    
GetTypeCode Method     System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()                                                                         
ToBoolean   Method     bool IConvertible.ToBoolean(System.IFormatProvider provider)                                                                                      
ToByte      Method     byte IConvertible.ToByte(System.IFormatProvider provider)                                                                                         
ToChar      Method     char IConvertible.ToChar(System.IFormatProvider provider)                                                                                         
ToDateTime  Method     datetime IConvertible.ToDateTime(System.IFormatProvider provider)                                                                                 
ToDecimal   Method     decimal IConvertible.ToDecimal(System.IFormatProvider provider)                                                                                   
ToDouble    Method     double IConvertible.ToDouble(System.IFormatProvider provider)                                                                                     
ToInt16     Method     int16 IConvertible.ToInt16(System.IFormatProvider provider)                                                                                       
ToInt32     Method     int IConvertible.ToInt32(System.IFormatProvider provider)                                                                                         
ToInt64     Method     long IConvertible.ToInt64(System.IFormatProvider provider)                                                                                        
ToSByte     Method     sbyte IConvertible.ToSByte(System.IFormatProvider provider)                                                                                       
ToSingle    Method     float IConvertible.ToSingle(System.IFormatProvider provider)                                                                                      
ToString    Method     string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormatProvider provider)                
ToType      Method     System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)                                                           
ToUInt16    Method     uint16 IConvertible.ToUInt16(System.IFormatProvider provider)                                                                                     
ToUInt32    Method     uint32 IConvertible.ToUInt32(System.IFormatProvider provider)                                                                                     
ToUInt64    Method     uint64 IConvertible.ToUInt64(System.IFormatProvider provider)                                                                                     


   TypeName: iTextSharp.text.Document

Name                        MemberType   Definition                                                                                                                      
----                        ----------   ----------                                                                                                                      
Add                         Method       bool Add(iTextSharp.text.IElement element), bool IElementListener.Add(iTextSharp.text.IElement element)                         
AddAuthor                   Method       bool AddAuthor(string author)                                                                                                   
AddCreationDate             Method       bool AddCreationDate()                                                                                                          
AddCreator                  Method       bool AddCreator(string creator)                                                                                                 
AddDocListener              Method       void AddDocListener(iTextSharp.text.IDocListener listener)                                                                      
AddHeader                   Method       bool AddHeader(string name, string content)                                                                                     
AddKeywords                 Method       bool AddKeywords(string keywords)                                                                                               
AddLanguage                 Method       bool AddLanguage(string language)                                                                                               
AddProducer                 Method       bool AddProducer()                                                                                                              
AddSubject                  Method       bool AddSubject(string subject)                                                                                                 
AddTitle                    Method       bool AddTitle(string title)                                                                                                     
Close                       Method       void Close(), void IDocListener.Close()                                                                                         
CloseDocument               Method       void CloseDocument()                                                                                                            
Dispose                     Method       void Dispose(), void IDisposable.Dispose()                                                                                      
Equals                      Method       bool Equals(System.Object obj)                                                                                                  
GetAccessibleAttribute      Method       iTextSharp.text.pdf.PdfObject GetAccessibleAttribute(iTextSharp.text.pdf.PdfName key), iTextSharp.text.pdf.PdfObject IAccessi...
GetAccessibleAttributes     Method       System.Collections.Generic.Dictionary[iTextSharp.text.pdf.PdfName,iTextSharp.text.pdf.PdfObject] GetAccessibleAttributes(), S...
GetBottom                   Method       float GetBottom(float margin)                                                                                                   
GetHashCode                 Method       int GetHashCode()                                                                                                               
GetLeft                     Method       float GetLeft(float margin)                                                                                                     
GetRight                    Method       float GetRight(float margin)                                                                                                    
GetTop                      Method       float GetTop(float margin)                                                                                                      
GetType                     Method       type GetType()                                                                                                                  
IsMarginMirroring           Method       bool IsMarginMirroring()                                                                                                        
IsOpen                      Method       bool IsOpen()                                                                                                                   
NewPage                     Method       bool NewPage(), bool IDocListener.NewPage()                                                                                     
Open                        Method       void Open(), void IDocListener.Open()                                                                                           
OpenDocument                Method       void OpenDocument()                                                                                                             
RemoveIDocListener          Method       void RemoveIDocListener(iTextSharp.text.IDocListener listener)                                                                  
ResetPageCount              Method       void ResetPageCount(), void IDocListener.ResetPageCount()                                                                       
SetAccessibleAttribute      Method       void SetAccessibleAttribute(iTextSharp.text.pdf.PdfName key, iTextSharp.text.pdf.PdfObject value), void IAccessibleElement.Se...
SetMarginMirroring          Method       bool SetMarginMirroring(bool marginMirroring), bool IDocListener.SetMarginMirroring(bool marginMirroring)                       
SetMarginMirroringTopBottom Method       bool SetMarginMirroringTopBottom(bool marginMirroringTopBottom), bool IDocListener.SetMarginMirroringTopBottom(bool marginMir...
SetMargins                  Method       bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom), bool IDocListener.SetMargins(float...
SetPageSize                 Method       bool SetPageSize(iTextSharp.text.Rectangle pageSize), bool IDocListener.SetPageSize(iTextSharp.text.Rectangle pageSize)         
ToString                    Method       string ToString()                                                                                                               
Bottom                      Property     float Bottom {get;}                                                                                                             
BottomMargin                Property     float BottomMargin {get;}                                                                                                       
HtmlStyleClass              Property     string HtmlStyleClass {get;set;}                                                                                                
ID                          Property     iTextSharp.text.AccessibleElementId ID {get;set;}                                                                               
IsInline                    Property     bool IsInline {get;}                                                                                                            
JavaScript_onLoad           Property     string JavaScript_onLoad {get;set;}                                                                                             
JavaScript_onUnLoad         Property     string JavaScript_onUnLoad {get;set;}                                                                                           
Left                        Property     float Left {get;}                                                                                                               
LeftMargin                  Property     float LeftMargin {get;}                                                                                                         
PageCount                   Property     int PageCount {set;}                                                                                                            
PageNumber                  Property     int PageNumber {get;}                                                                                                           
PageSize                    Property     iTextSharp.text.Rectangle PageSize {get;}                                                                                       
Right                       Property     float Right {get;}                                                                                                              
RightMargin                 Property     float RightMargin {get;}                                                                                                        
Role                        Property     iTextSharp.text.pdf.PdfName Role {get;set;}                                                                                     
Top                         Property     float Top {get;}                                                                                                                
TopMargin                   Property     float TopMargin {get;}                                                                                                          
AddParagraph                ScriptMethod System.Object AddParagraph();                                                                                                   

And here are the members of $Document:

   TypeName: iTextSharp.text.Document

Name                        MemberType Definition                                                                                                                        
----                        ---------- ----------                                                                                                                        
Add                         Method     bool Add(iTextSharp.text.IElement element), bool IElementListener.Add(iTextSharp.text.IElement element)                           
AddAuthor                   Method     bool AddAuthor(string author)                                                                                                     
AddCreationDate             Method     bool AddCreationDate()                                                                                                            
AddCreator                  Method     bool AddCreator(string creator)                                                                                                   
AddDocListener              Method     void AddDocListener(iTextSharp.text.IDocListener listener)                                                                        
AddHeader                   Method     bool AddHeader(string name, string content)                                                                                       
AddKeywords                 Method     bool AddKeywords(string keywords)                                                                                                 
AddLanguage                 Method     bool AddLanguage(string language)                                                                                                 
AddProducer                 Method     bool AddProducer()                                                                                                                
AddSubject                  Method     bool AddSubject(string subject)                                                                                                   
AddTitle                    Method     bool AddTitle(string title)                                                                                                       
Close                       Method     void Close(), void IDocListener.Close()                                                                                           
CloseDocument               Method     void CloseDocument()                                                                                                              
Dispose                     Method     void Dispose(), void IDisposable.Dispose()                                                                                        
Equals                      Method     bool Equals(System.Object obj)                                                                                                    
GetAccessibleAttribute      Method     iTextSharp.text.pdf.PdfObject GetAccessibleAttribute(iTextSharp.text.pdf.PdfName key), iTextSharp.text.pdf.PdfObject IAccessibl...
GetAccessibleAttributes     Method     System.Collections.Generic.Dictionary[iTextSharp.text.pdf.PdfName,iTextSharp.text.pdf.PdfObject] GetAccessibleAttributes(), Sys...
GetBottom                   Method     float GetBottom(float margin)                                                                                                     
GetHashCode                 Method     int GetHashCode()                                                                                                                 
GetLeft                     Method     float GetLeft(float margin)                                                                                                       
GetRight                    Method     float GetRight(float margin)                                                                                                      
GetTop                      Method     float GetTop(float margin)                                                                                                        
GetType                     Method     type GetType()                                                                                                                    
IsMarginMirroring           Method     bool IsMarginMirroring()                                                                                                          
IsOpen                      Method     bool IsOpen()                                                                                                                     
NewPage                     Method     bool NewPage(), bool IDocListener.NewPage()                                                                                       
Open                        Method     void Open(), void IDocListener.Open()                                                                                             
OpenDocument                Method     void OpenDocument()                                                                                                               
RemoveIDocListener          Method     void RemoveIDocListener(iTextSharp.text.IDocListener listener)                                                                    
ResetPageCount              Method     void ResetPageCount(), void IDocListener.ResetPageCount()                                                                         
SetAccessibleAttribute      Method     void SetAccessibleAttribute(iTextSharp.text.pdf.PdfName key, iTextSharp.text.pdf.PdfObject value), void IAccessibleElement.SetA...
SetMarginMirroring          Method     bool SetMarginMirroring(bool marginMirroring), bool IDocListener.SetMarginMirroring(bool marginMirroring)                         
SetMarginMirroringTopBottom Method     bool SetMarginMirroringTopBottom(bool marginMirroringTopBottom), bool IDocListener.SetMarginMirroringTopBottom(bool marginMirro...
SetMargins                  Method     bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom), bool IDocListener.SetMargins(float m...
SetPageSize                 Method     bool SetPageSize(iTextSharp.text.Rectangle pageSize), bool IDocListener.SetPageSize(iTextSharp.text.Rectangle pageSize)           
ToString                    Method     string ToString()                                                                                                                 
Bottom                      Property   float Bottom {get;}                                                                                                               
BottomMargin                Property   float BottomMargin {get;}                                                                                                         
HtmlStyleClass              Property   string HtmlStyleClass {get;set;}                                                                                                  
ID                          Property   iTextSharp.text.AccessibleElementId ID {get;set;}                                                                                 
IsInline                    Property   bool IsInline {get;}                                                                                                              
JavaScript_onLoad           Property   string JavaScript_onLoad {get;set;}                                                                                               
JavaScript_onUnLoad         Property   string JavaScript_onUnLoad {get;set;}                                                                                             
Left                        Property   float Left {get;}                                                                                                                 
LeftMargin                  Property   float LeftMargin {get;}                                                                                                           
PageCount                   Property   int PageCount {set;}                                                                                                              
PageNumber                  Property   int PageNumber {get;}                                                                                                             
PageSize                    Property   iTextSharp.text.Rectangle PageSize {get;}                                                                                         
Right                       Property   float Right {get;}                                                                                                                
RightMargin                 Property   float RightMargin {get;}                                                                                                          
Role                        Property   iTextSharp.text.pdf.PdfName Role {get;set;}                                                                                       
Top                         Property   float Top {get;}                                                                                                                  
TopMargin                   Property   float TopMargin {get;}                                                                                                            
Chavez
  • 190
  • 12

1 Answers1

1

Your function returns two objects, first is boolean and second iTextSharp.text.Document object. Add-Member doesn't return a value unless -PassThru parameter is set, and even in that case the value would be the object, not a boolean.

So one of the iTextSharp.text.Document methods you call is returning $true or $false. You could redirect return values to $null to fix the issue:

$Document.SetPageSize([iTextSharp.text.PageSize]::A4) | Out-Null
$Document.SetMargins(5,5,5,5) | Out-Null
$Document.AddAuthor($env:USERNAME) | Out-Null
Janne Tuukkanen
  • 1,620
  • 9
  • 13
  • Awesome. I suspected this, but it felt really silly to me that any of these methods from itextsharp would return a `boolean` so I naively dismissed that right away. The problem is fixed, thank you. – Chavez Mar 07 '18 at 14:14