Database Reference
In-Depth Information
Creating reusable script modules
(Advanced)
In this recipe we'll turn prior script libraries into more reusable script modules.
Getting ready
Complete the previous recipes irst. See sample code iles or prior recipes for additional
functions such as
Get-DataTable
and
Set-CommandParamsFromArray
.
How to do it...
1.
Evaluate PowerShell module paths with
$env:PSModulePath
:
PS > $env:PSModulePath.Split(";")
C:\Users\Geoff\Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
2. In your
%USERPROFILE%\Documents\WindowsPowerShell\Modules\
folder,
create the folder
Oracle.DataAccess
(create the
Modules
folder if it does
not exist).
3. In the
Oracle.DataAccess
folder create the ile
Oracle.DataAccess.psm1
(note the
psm1
extension, rather than
ps1
).
4. At the top of the ile deine a parameter for the version of ODP.NET to use, a variable
to save a connection object, and a
Load
function:
param ([Parameter(Mandatory=$true)][validateset(2,4)]
[int] $OdpVersion)
$SCRIPT:conn = $null
function Load {
param (
[Parameter(Position=0, Mandatory=$true)]
[validateset(2,4)] [int] $version,
[Parameter(Position=1)] [switch] $passThru
)
$name = ("Oracle.DataAccess, Version={0}.112.3.0,
Culture=neutral, PublicKeyToken=89b483f429c47342" -f $version)
$asm = [System.Reflection.Assembly]::Load($name)
if ($passThru) { $asm }
}
Search WWH ::
Custom Search