Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

Invoking PowerShell from WScript via COM

4 vues
Accéder directement au premier message non lu

Joris van Lier

non lue,
18 mars 2007, 15:49:2718/03/2007
à
Hi all, i'm m trying to do something that doesn't seem possible using default powershell and i'm hitting a bump ?with setting my interface as the default one? / ignorance about COM.
As per the subject i'm trying to expose RunspaceInvoke to WScript via COM Interop, to do that i've created an interface and a class that implements the interface and decorated these with com interop attributes, i sign the assembly and use regasm with the /tlb and /codebase parameters to register my assembly.
This works from powershell

PS> $psi = new-object -comObject PSInvoker; $psi.Invoke("Get-Help")

returns the output of the invoked command as a string. However from WScript i cannot use the method, in a VBScript i try

Dim psi
Set psi = CreateObject("PSInvoker")
WScript.Echo psi.Invoke("Get-Help")

and the PSInvoker object Can be created, but i think it doesn't bind to the right interface because the 3rd line errs

Object doesn't support this property or method: 'psi.Invoke'

When looking at the published COM Object using OLE Viewer i can see that the _Object interface is the first interface.

My first question obviously is: Should i at all try to do this, i guess there the smart people on the PowerShell team have a reason not to expose RunspaceInvoke to COM
Second: Is there a way to expose a .NET interface to or consume such an interface from COM clients like WScript?

C# Code for PSInvoker

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Runtime.InteropServices;

[ComImport()]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid(PSInvoker.InterfaceId)]
public interface _PSInvoker
{
[PreserveSig()]
string Invoke(string script);
[PreserveSig()]
PSObject[] InvokeGetArray(string script);
}
[ComVisible(true)]
[ProgId(PSInvoker.ProgId)]
[Guid(PSInvoker.ClassId)]
[ClassInterface(ClassInterfaceType.None)]
public class PSInvoker : _PSInvoker
{
// Implements _PSInvoker
// Use the Region directive to define a section named COM Guids.
#region "COM GUIDs"
// These GUIDs provide the COM identity for this class
// and its COM interfaces. You can generate
// these guids using guidgen.exe
public const string ClassId = "D697F920-2886-4DF9-B7C0-68712C60B254";
public const string InterfaceId = "56C7535B-27BD-4E8B-8512-253C97FC96C3";
public const string EventsId = "FC367EC7-8F48-4044-971D-4638BAFC2334";
public const string ProgId = "PSInvoker";
#endregion
private RunspaceInvoke runspaceInvoke;
public PSInvoker() : base()
{
runspaceInvoke = new RunspaceInvoke();
}

//Implements _PSInvoker.Invoke
public string Invoke(string script)
{
Collection<PSObject> colResults = new Collection<PSObject>();
colResults = runspaceInvoke.Invoke(script);
string strResult = String.Empty;
foreach (PSObject obj in colResults) {
strResult += obj.ToString();
}
return strResult;
}
//Implements _PSInvoker.InvokeGetArray
public PSObject[] InvokeGetArray(string script)
{
Collection<PSObject> colResults = new Collection<PSObject>();
colResults = runspaceInvoke.Invoke(script);
PSObject[] arrResults = new PSObject[colResults.Count + 1];
colResults.CopyTo(arrResults, 0);
return arrResults;
}
}

Joris van Lier

non lue,
28 mars 2007, 10:53:2328/03/2007
à
Joris van Lier wrote:
> Hi all, i'm m trying to do something that doesn't seem possible using
> default powershell and i'm hitting a bump ?with setting my interface
> as the default one? / ignorance about COM.
> As per the subject i'm trying to expose RunspaceInvoke to WScript via
> COM Interop, to do that i've created an interface and a class that
> implements the interface and decorated these with com interop
> attributes, i sign the assembly and use regasm with the /tlb and
> /codebase parameters to register my assembly.
> This works from powershell
>

I'm still interested in invoking .NET COM objects from WScript, my problems probably have more to do with COM in general that with Powershell, so i'll take this question to a .NET interop group, but would still appreciate any insight from the powershell side of things.

Joris

0 nouveau message