Last active
September 17, 2019 20:00
-
-
Save MyITGuy/78191133d108ad73b7570dd5b1cf965a to your computer and use it in GitHub Desktop.
Use PowerShell to test for the existence of a COM Object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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