Skip to content

Invoke COM method in Powershell

December 12, 2011

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.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: