-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRemove-ComObject.ps1
More file actions
116 lines (87 loc) · 3.14 KB
/
Remove-ComObject.ps1
File metadata and controls
116 lines (87 loc) · 3.14 KB
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
<#
.Synopsis
Release COM object and remove associated variable.
.Description
Release COM object and remove associated variable.
COM object is released using [System.Runtime.InteropServices.Marshal]::FinalReleaseComObject method call.
Optionally you can force garbage collection.
.Parameter Name
Array of variables names, that contain COM objects.
.Parameter Force
Switch. Allows the function to remove a variable even if it is read-only.
Note, that even using the Force parameter, the function cannot remove a constant.
.Parameter ForceGC
Switch. Force garbage collection after removing all COM objects and associated variables.
.Example
Remove-ComObject -Name Ie
Removes COM object stored in variable $Ie and variable itself.
Example:
# Create Internet Explorer COM object
$Ie = New-Object -ComObject InternetExplorer.Application
# ... do stuff ...
# Remove Internet Explorer COM object
Remove-ComObject -Name Ie
.Example
'Ie' | Remove-ComObject
Removes COM object stored in variable $Ie and variable itself.
Variable name is supplied via pipeline.
.Example
Remove-ComObject -Name Ie, Excel, Word
Removes COM objects stored in variables $Ie, $Excel, and $Word and variables themselves.
.Example
'Ie', 'Excel', 'Word' | Remove-ComObject
Removes COM objects stored in variables $Ie, $Excel, and $Word and variables themselves.
Variable names are supplied via pipeline.
.Example
Remove-ComObject -Name Ie -ForceGC
Removes COM object stored in variable $Ie and variable itself.
Force immediate garbage collection after variable and COM object has been removed.
.Example
Remove-ComObject -Name Ie -Force
Removes COM object stored in variable $Ie and variable itself.
Variable removed even if it's ReadOnly.
Example:
# Create Internet Explorer COM object
New-Variable -Name Ie -Option ReadOnly -Value (New-Object -ComObject InternetExplorer.Application)
# ... do stuff ...
# Remove Internet Explorer COM object
Remove-ComObject -Name Ie -Force
#>
function Remove-ComObject
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]]$Name,
[switch]$Force,
[switch]$ForceGC
)
Process
{
foreach($var in $Name)
{
Write-Verbose 'Trying to release COM object'
if
(
[System.Runtime.InteropServices.Marshal]::FinalReleaseComObject(
[System.__ComObject](Get-Variable -Name $var -Scope 1 -ValueOnly -ErrorAction Stop)
)
)
{
Write-Error 'Failed to release COM object!'
}
Write-Verbose "Removing variable from parent scope: $var"
Remove-Variable -Name $var -Scope 1 -Force:$Force
}
}
End
{
if($ForceGC)
{
Write-Verbose 'Forcing garbage collection'
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
}