-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnect-SRUMDB.ps1
executable file
·93 lines (83 loc) · 4.16 KB
/
Connect-SRUMDB.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<#
.NOTES
Version: 1.0
Author: Thomas V Fischer
Creation Date: 20190315
Purpose/Change: Initial script development
.SYNOPSIS
Script: Get-SRUMDB
Open the SRUM database with ESENT DLL
.DESCRIPTION
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
Function Connect-SRUMDB{
Param(
[Parameter(Position=0,Mandatory = $true,
ParameterSetName = "SRUMDBPath")]
[ValidateNotNull()]
$srumdbpath
)
Begin{
}
Process{
#set-up a state condition variable maybe in the future we can expand on state
$JETState="OK"
Try{
# we are assuming the EsentDll is already loaded
## Point the JET methods to the database file
[System.Int32]$FileType = -1
[System.Int32]$PageSize = -1
[Microsoft.Isam.Esent.Interop.Api]::JetGetDatabaseFileInfo($srumdbpath, [ref]$PageSize, [Microsoft.Isam.Esent.Interop.JET_DbInfo]::PageSize)
[Microsoft.Isam.Esent.Interop.Api]::JetGetDatabaseFileInfo($srumdbpath, [ref]$FileType, [Microsoft.Isam.Esent.Interop.JET_DbInfo]::FileType)
[Microsoft.Isam.Esent.Interop.JET_filetype]$DBType = [Microsoft.Isam.Esent.Interop.JET_filetype]($FileType)
# Need to check if we have database DBType (Will add later)
## To access the database we need to open a JET session
[Microsoft.Isam.Esent.Interop.JET_INSTANCE]$Instance = New-Object -TypeName Microsoft.Isam.Esent.Interop.JET_INSTANCE
[Microsoft.Isam.Esent.Interop.JET_SESID]$Session = New-Object -TypeName Microsoft.Isam.Esent.Interop.JET_SESID
$Temp = [Microsoft.Isam.Esent.Interop.Api]::JetSetSystemParameter($Instance, [Microsoft.Isam.Esent.Interop.JET_SESID]::Nil, [Microsoft.Isam.Esent.Interop.JET_param]::DatabasePageSize, $PageSize, $null)
$Temp = [Microsoft.Isam.Esent.Interop.Api]::JetSetSystemParameter($Instance, [Microsoft.Isam.Esent.Interop.JET_SESID]::Nil, [Microsoft.Isam.Esent.Interop.JET_param]::Recovery, [int]$Recovery, $null)
$Temp = [Microsoft.Isam.Esent.Interop.Api]::JetSetSystemParameter($Instance, [Microsoft.Isam.Esent.Interop.JET_SESID]::Nil, [Microsoft.Isam.Esent.Interop.JET_param]::CircularLog, [int]$CircularLogging, $null)
[Microsoft.Isam.Esent.Interop.Api]::JetCreateInstance2([ref]$Instance, "Instance", "Instance", [Microsoft.Isam.Esent.Interop.CreateInstanceGrbit]::None)
$Temp = [Microsoft.Isam.Esent.Interop.Api]::JetInit2([ref]$Instance, [Microsoft.Isam.Esent.Interop.InitGrbit]::None)
[Microsoft.Isam.Esent.Interop.Api]::JetBeginSession($Instance, [ref]$Session, $UserName, $Password)
## Ok Now open the database
[Microsoft.Isam.Esent.Interop.JET_DBID]$DatabaseId = New-Object -TypeName Microsoft.Isam.Esent.Interop.JET_DBID
$Temp = [Microsoft.Isam.Esent.Interop.Api]::JetAttachDatabase($Session, $Path, [Microsoft.Isam.Esent.Interop.AttachDatabaseGrbit]::ReadOnly)
$Temp = [Microsoft.Isam.Esent.Interop.Api]::JetOpenDatabase($Session, $Path, $Connect, [ref]$DatabaseId, [Microsoft.Isam.Esent.Interop.OpenDatabaseGrbit]::ReadOnly)
# $DatabaseId
# $Session
# $Connect ### Important tells us if the database is accessible
#Check the session information
}
Catch{
# Something went wrong so return a big no way
$JETState="Connection FAILED"
Write-Warning "Unable to connect to the database ${path}"
$dbconnect = [PSCustomObject]@{
JETState=$JETState}
Break
}
$dbconnect = [PSCustomObject]@{
JETState=$JETState;
Instance=$Instance;
Session=$Session;
DatabaseId=$DatabaseId;
Path=$Path;
ConnectState=$Connect;
DBType=$DBType;
DBPageSize=$PageSize; DBFileType=$FileType}
#Write-Output -InputObject ($dbconnect)
Write-Host "Connection to ${path} status is ${JETState}"
Return $dbconnect
}
End{
}
}