InstallAddComType.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
$cp = New-Object CodeDom.Compiler.CompilerParameters 
$cp.CompilerOptions = "/unsafe"
$null =$cp.ReferencedAssemblies.Add([object].Assembly.Location)
$null = $cp.ReferencedAssemblies.Add([psobject].Assembly.Location)

Add-Type -PassThru -CompilerParameters $cp -TypeDefinition @"
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Management.Automation;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
 
 
namespace n$(Get-Random)
{
    [Cmdlet("Add", "ComType")]
    public class TestCmdletCommand : PSCmdlet
    {
        private string library;
         
        [Parameter(Mandatory=true,ParameterSetName="Library",ValueFromPipelineByPropertyName=true)]
        [Alias("FullName")]
        public string Library
        {
            get { return library; }
            set { this.library = value; }
        }
         
        private SwitchParameter passThru;
         
        [Parameter(ParameterSetName="Library",ValueFromPipelineByPropertyName=true)]
        public SwitchParameter PassThru
        {
            get { return this.passThru; }
            set { this.passThru = value; }
        }
 
        private enum RegKind
        {
            RegKind_Default = 0,
            RegKind_Register = 1,
            RegKind_None = 2
        }
     
        [DllImport( "oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false )]
        private static extern void LoadTypeLibEx( String strTypeLibName, RegKind regKind,
        [MarshalAs( UnmanagedType.Interface )] out Object typeLib );
 
        protected override void BeginProcessing()
        {
        }
         
        protected override void ProcessRecord()
        {
            Object typeLib;
            LoadTypeLibEx( this.Library, RegKind.RegKind_None, out typeLib );
             
            if( typeLib == null )
            {
                ErrorRecord err = new ErrorRecord(new Exception("Could not load library:" + this.Library),"ExportIsoCommand.CouldNotLoad", ErrorCategory.OpenError, this.Library);
                WriteError(err);
                return;
            }
             
            TypeLibConverter converter = new TypeLibConverter();
            ConversionEventHandler eventHandler = new ConversionEventHandler();
            int lastSlash = this.Library.LastIndexOf("\\");
            string libName = this.Library;
            if (lastSlash != -1) {
                libName = this.Library.Substring(lastSlash + 1);
            }
            AssemblyBuilder asm = converter.ConvertTypeLibToAssembly( typeLib, libName, 0, eventHandler, null, null, null, null );
            if (this.passThru) {
                this.WriteObject(asm.GetTypes(),true);
            }
             
        }
                 
        protected override void EndProcessing()
        {
         
        }
 
    }
     
    class ConversionEventHandler : ITypeLibImporterNotifySink
    {
        public void ReportEvent( ImporterEventKind eventKind, int eventCode, string eventMsg )
        {
            // handle warning event here...
        }
         
        public Assembly ResolveRef( object typeLib )
        {
            // resolve reference here and return a correct assembly...
            return null;
        }
    }
}
"@
 |
    Select-object -expandProperty Assembly |
    Import-module