Invoke COM method in Powershell
Today I was writing a script that would execute all SQL scripts in some folder and for that I needed to connect to the database. Where I work, we have a COM wrapped VBscript that gives Connection String and that is what I needed.
So lets see how to get it in Powershell:
First we need an wsc script containing data
<package id="config"> <component> <script language="VBScript"> Function GetDBConnectionString() GetDBConnectionString = "connectionStringOfMike" End Function </script> <registration description="MikesConfig" progid="config.wsc" version="1.00"> </registration> <public> <method name="GetDBConnectionString" dispid="1"/> </public> </component> </package>
and now to get it in Powershell, just execute this:
$config = New-Object -com config.wsc $DBConnectionString = $config.psbase.GetType().InvokeMember('GetDBConnectionString', [Reflection.BindingFlags]::InvokeMethod, $null, $config, $null)
and you have a connection string generated by your COM method.