Skip to content

Instantly share code, notes, and snippets.

@MyITGuy
Last active September 17, 2019 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MyITGuy/78191133d108ad73b7570dd5b1cf965a to your computer and use it in GitHub Desktop.
Save MyITGuy/78191133d108ad73b7570dd5b1cf965a to your computer and use it in GitHub Desktop.
Use PowerShell to test for the existence of a COM Object.
#region Test-ComObject
function Test-ComObject {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
[string]
$ComputerName = $env:COMPUTERNAME
,
[Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
[string]
$ComObject
)
begin {
Write-Verbose $MyInvocation.MyCommand
}
process {
try {
$Type = [System.Type]::GetTypeFromProgID($ComObject, $ComputerName, $true)
}
catch {
}
finally {
if ($Type) {
$true
}
else {
$false
}
}
}
end {
}
}
#endregion Test-ComObject
#region Test-ComObject
function Test-ComObject {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
[string]
$ComputerName
,
[Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
[string]
$ComObject
)
begin {
Write-Verbose $MyInvocation.MyCommand
}
process {
try {
if ($ComputerName) {
$key = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.Registry]::ClassesRoot , $ComputerName)
}
else {
$key = [Microsoft.Win32.Registry]::ClassesRoot
}
$subkey = $key.OpenSubKey("$($ComObject)\CLSID", $false)
Set-Variable -Name "CLSID" -Value ([System.Guid]::Empty) -Scope Local
[System.Guid]::TryParse($subkey.GetValue(''), [ref]$CLSID) | Out-Null
}
catch {
# Throw $error[0]
}
finally {
($CLSID -ne '00000000-0000-0000-0000-000000000000')
}
}
end {
}
}
#endregion Test-ComObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment