3

I'm very new to COM and Windows programming/scripting in general. What I was trying to do is scripting Windows Live Writer; according to the documentation before I can call

  $o = New-Object -c WindowsLiveWriter.Application

I need to load the TLB first, so I should call the add-type command, unfortunately it fails:

PS C:\Users\NoWhereMan> add-type windowslivewriter.application
Add-Type : c:\Users\NoWhereMan\AppData\Local\Temp\a7ifbimo.0.cs(1) : A namespace does not directly contain members such
 as fields or methods
c:\Users\NoWhereMan\AppData\Local\Temp\a7ifbimo.0.cs(1) : >>> windowslivewriter.application
At line:1 char:9
+ add-type <<<<  windowslivewriter.application
    + CategoryInfo          : InvalidData: (c:\Users\NoWher...elds or methods:CompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. There were compilation errors.
At line:1 char:9
+ add-type <<<<  windowslivewriter.application
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

for what it's worth, I'm running Windows7 x64

EDIT: x64 was the key issue, I needed to run PSH as a x86 process

Thanks

2
  • 1
    You shouldn't need to use add-type for a COM object it should infer members from the objects IDispatch interface. AFAIK add-type is only for adding a new .NET assembly. Commented Feb 14, 2010 at 11:09
  • as I commented to Richard, I was mislead by the error I got from new-object which was in fact thrown because I was using a 64 bit PSH process. Thank you! Commented Feb 14, 2010 at 11:15

1 Answer 1

4

From help add-type:

Adds a Microsoft .NET Framework type (a class) to a Windows PowerShell session.

but windowslivewriter.application is not a .NET type.

PowerShell (PSH) directly supports COM objects, you do not need to take any special steps to load the Type Library (TLB)1, just call the methods diretcly as given in the documentation for the component. E.g.:

$lw = New-Object -com WindowsLiveWriter.Application   
$lw.NetPost()

to launch the new post editor.

Summary: you do not need to load the TLB first.

Under 64bit Windows, you might need to ensure you are running a 32bit instance ("x86") of PSH to do this (depending if the Live Writer component runs in or out of process).


1 Strictly speaking, this only applies to COM types that support scripting with IDispatch, but in practice there are few that don't.

Sign up to request clarification or add additional context in comments.

1 Comment

I was mislead by the error message I got with New-Object -c ; but in fact it was thrown because I was running a x64 instance of PSH instead of x86, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.