-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCOMHelper.ps1
119 lines (104 loc) · 2.43 KB
/
COMHelper.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
108
109
110
111
112
113
114
115
116
117
118
$source = @'
using System;
using System.Runtime.InteropServices;
public class Ole32
{
[DllImport("ole32.dll")]
public static extern int ProgIDFromCLSID([In()]ref Guid clsid, [MarshalAs(UnmanagedType.LPWStr)]out string lplpszProgID);
[DllImport("ole32.dll")]
public static extern int CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)]string lpszProgID, out Guid lpcslid);
}
'@
Add-Type $source -ErrorAction:SilentlyContinue
function Get-ProgID {
<#
.SYNOPSIS
Retrieves the ProgID for a given CLSID
.DESCRIPTION
Retrieves the ProgID for a given CLSID
.EXAMPLE
Get-ProgID "Word.Application","Excel.Application"
.EXAMPLE
Get-ProgID -ClassID "Word.Application","Excel.Application"
.PARAMETER ClassId
The Class ID (CLSID) for which you want to retrieve the Prog ID
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='The Class ID (Guid) for which you want to retreive the Prog ID')]
[Alias('Guid')]
[guid[]]$ClassId
)
begin
{
Write-Verbose "Retreiving Prog ID for $ClassId"
}
process
{
ForEach ($item in $ClassId)
{
Write-Verbose "Processing $item"
[string]$progId = [string]::Empty
$hr = [Ole32]::ProgIDFromCLSID([ref]$item, [ref]$progId)
if ($hr -eq 0)
{
$progId
}
else
{
throw New-Object System.ComponentModel.Win32Exception($hr)
}
}
}
}
function Get-ClassID {
<#
.SYNOPSIS
Retrieves the ProgID for a given CLSID
.DESCRIPTION
Retrieves the ProgID for a given Class ID
.EXAMPLE
Get-ClassID "000209ff-0000-0000-c000-000000000046"
.EXAMPLE
#todo
.PARAMETER ProgId
The Program Id (ProgID) for which you want to retrieve the Class ID
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='The Progr ID for which you want to retreive the Class ID')]
[string[]]$ProgId
)
begin
{
Write-Verbose "Retreiving Class Id for $ProgId"
}
process
{
ForEach ($item in $ProgId)
{
Write-Verbose "Processing $item"
[Guid]$clsId = [Guid]::Empty
$hr = [Ole32]::CLSIDFromProgId($item, [ref]$clsid)
if ($hr -eq 0)
{
$clsid
}
else
{
throw New-Object System.ComponentModel.Win32Exception($hr)
}
}
}
}
#examples:
#Get-ClassID "Word.Application"
#Get-ClassID "Word.Application" | Get-ProgID