通过PowerShell加载类型库并编写Windows Live Writer脚本 [英] Loading a Type Library via PowerShell and scripting Windows Live Writer
问题描述
总体来说,我对COM和Windows编程/脚本非常陌生。我试图做的是编写Windows Live Writer脚本。根据文档,然后我才能调用
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
我需要先加载TLB,所以我应该调用 add-type
命令,不幸的是它失败了:
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
,我正在运行Windows7 x64
编辑:x64是关键问题,我需要将PSH作为x86进程运行
EDIT: x64 was the key issue, I needed to run PSH as a x86 process
谢谢
推荐答案
从帮助添加类型
:
将Microsoft .NET Framework类型(一个类)添加到Windows PowerShell会话中。
Adds a Microsoft .NET Framework type (a class) to a Windows PowerShell session.
但 windowslivewriter.application
不是.NET类型。
PowerShell(PSH)直接支持COM对象,不需要带加载类型库(TLB) 1 的任何特殊步骤,只需按照文档。例如:
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()
启动新的帖子编辑器。
摘要:您不需要不需要首先加载TLB。
Summary: you do not need to load the TLB first.
在64位Windows下,您可能需要确保正在运行32位实例( x86 ),以执行此操作(取决于Live Writer组件是在进程中运行还是在进程外运行)。
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 严格来说,这仅适用于支持使用IDispatch编写脚本的COM类型,但实际上很少有不支持的类型。
1 Strictly speaking, this only applies to COM types that support scripting with IDispatch, but in practice there are few that don't.
这篇关于通过PowerShell加载类型库并编写Windows Live Writer脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!