Given the following modules:
adonetlib.psml
param($ModelModule)
$dbmodel=import-module $ModelModule -AsCustomObject
function new-ADOnetconnection{
return $dbmodel.newconnection()
}
mysqlmodel.psm1
function newconnection{
write-host "MySQL connection"
}
sqlservermodel.psm1
function newconnection{
write-host "SQLServer connection"
}
I’d like to be able to do this:
$mysql= import-module adonetlib -argumentlist mysqlmodel -ascustomobject
$sqlserver= import-module adonetlib -argumentlist sqlservermodel -ascustomobject
and have the following be true (sorry about the syntax):
$mysql."new-adonetconnection"() -> returns "MySql connection"
$sqlsever."new-adonetconnection"() -> returns "SQLServer connection"
Apparently, though, the adonetlib module is only loaded once (even though we imported it with different arguments). Both statements return “MySQL connection”.
Also,
get-module -all
shows that the mysqlmodel module is loaded, but sqlservermodel isn’t.
Any ideas about how I can get this to work?
The functionality you’re looking for may be present already in the Powershell Community Extensions:
But in case you’re implementing just for fun:
You might be better off using submodules to implement the polymorphic behavior it looks like you’re trying to achieve. The top-level module would export a single New-ADOnetConnection cmdlet, with a parameter to select which DB you want to use for that connection; then it would dynamically choose which submodule to use to carry out the command.
If you’d like to avoid having to specify the DB on every call, you could create your own preference variable.