Skip to content

Commit 5e0ab15

Browse files
committed
Set the functions to export in manifest file
Changed parameter from array to string in Initiate-PdcVhdDisk Changed the check for the module configuration in all functions that needed it Added check for errors in Set-PdcConfiguration
1 parent 397a6b7 commit 5e0ab15

10 files changed

Lines changed: 466 additions & 51 deletions

PSDatabasClone.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
'New-PdcVhdDisk',
7171
'Remove-PdcDatabaseClone',
7272
'Remove-PdcDatabaseImage',
73-
'Set-PdcConfiguration'
73+
'Set-PdcConfiguration',
74+
'Test-PdcDatabaseSetup'
7475

7576
# Cmdlets to export from this module
7677
CmdletsToExport = ''

functions/Initialize-PdcVhdDisk.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Initialize-PdcVhdDisk {
4141

4242
Param(
4343
[Parameter(Mandatory = $true)]
44-
[string[]]$Path,
44+
[string]$Path,
4545
[System.Management.Automation.PSCredential]
4646
$Credential,
4747
[ValidateSet('GPT', 'MBR')]

functions/Invoke-PdcRepairClone.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ function Invoke-PdcRepairClone {
4848
)
4949

5050
begin {
51-
# Get the configurations for the program database
52-
$ecDatabaseName = Get-PSFConfigValue -FullName psdatabaseclone.database.name
53-
$ecDatabaseServer = Get-PSFConfigValue -FullName psdatabaseclone.database.Server
54-
5551
# Test the module database setup
56-
Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
52+
$result = Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
53+
54+
if(-not $result.Check){
55+
Stop-PSFFunction -Message $result.Message -Target $result -Continue
56+
return
57+
}
5758

5859
}
5960

functions/New-PdcDatabaseClone.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ function New-PdcDatabaseClone {
9494

9595
Write-PSFMessage -Message "Started image creation" -Level Output
9696

97-
# Get the configurations for the program database
98-
$ecDatabaseName = Get-PSFConfigValue -FullName psdatabaseclone.database.name
99-
$ecDatabaseServer = Get-PSFConfigValue -FullName psdatabaseclone.database.Server
100-
10197
# Test the module database setup
102-
Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
98+
$result = Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
99+
100+
if(-not $result.Check){
101+
Stop-PSFFunction -Message $result.Message -Target $result -Continue
102+
return
103+
}
103104

104105
# Random string
105106
$random = -join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_})

functions/New-PdcDatabaseImage.ps1

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,18 @@ function New-PdcDatabaseImage {
118118

119119
begin {
120120

121-
# Get the configurations for the program database
122-
$ecDatabaseName = Get-PSFConfigValue -FullName psdatabaseclone.database.name -Fallback "NotConfigured"
123-
$ecDatabaseServer = Get-PSFConfigValue -FullName psdatabaseclone.database.Server -Fallback "NotConfigured"
124-
125121
# Test the module database setup
126-
try{
127-
Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
128-
}
129-
catch{
130-
Stop-PSFFunction -Message "Something went wrong testing the module configuration" -ErrorRecord $_ -Target $SourceSqlInstance
122+
$result = Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
123+
124+
if(-not $result.Check){
125+
Stop-PSFFunction -Message $result.Message -Target $result -Continue
131126
return
132127
}
133128

129+
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server
130+
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
131+
132+
134133
Write-PSFMessage -Message "Started image creation" -Level Output
135134

136135
# Try connecting to the instance
@@ -265,7 +264,7 @@ function New-PdcDatabaseImage {
265264
try {
266265
Write-PSFMessage -Message "Initializing the vhd $imageName.vhd" -Level Verbose
267266

268-
$diskResult = Initialize-PdcVhdDisk -Path $vhdDisk.Path -Credential $DestinationCredential
267+
$diskResult = Initialize-PdcVhdDisk -Path $vhdPath -Credential $DestinationCredential
269268
}
270269
catch {
271270
Stop-PSFFunction -Message "Couldn't initialize vhd $vhdPath" -Target $imageName -ErrorRecord $_ -Continue
@@ -321,7 +320,7 @@ function New-PdcDatabaseImage {
321320
}
322321

323322
# Setup the temporary database name
324-
$tempDbName = "$($db.Name)-EasyClone"
323+
$tempDbName = "$($db.Name)-PSDatabaseClone"
325324

326325
# Restore database to image folder
327326
try {
@@ -374,10 +373,10 @@ function New-PdcDatabaseImage {
374373
@DatabaseName = '$databaseName', -- varchar(100)
375374
@DatabaseTimestamp = '$databaseTS' -- datetime
376375
"
377-
376+
Write-Host $query
378377
Write-PSFMessage -Message "Saving image information in database" -Level Verbose
379378

380-
Invoke-DbaSqlQuery -SqlInstance $ecDatabaseServer -Database $ecDatabaseName -Query $query
379+
Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
381380

382381
}
383382
catch {

functions/Remove-PdcDatabaseClone.ps1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Remove-PdcDatabaseClone -HostName Host1
6060
Removes all clones from Host1
6161
6262
#>
63+
[CmdLetBinding()]
64+
6365
param(
6466
[parameter(Mandatory = $true)]
6567
[ValidateNotNullOrEmpty()]
@@ -82,12 +84,13 @@ Removes all clones from Host1
8284
begin {
8385
Write-PSFMessage -Message "Started removing database clones" -Level Verbose
8486

85-
# Get the configurations for the program database
86-
$ecDatabaseName = Get-PSFConfigValue -FullName psdatabaseclone.database.name
87-
$ecDatabaseServer = Get-PSFConfigValue -FullName psdatabaseclone.database.server
88-
8987
# Test the module database setup
90-
Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
88+
$result = Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
89+
90+
if(-not $result.Check){
91+
Stop-PSFFunction -Message $result.Message -Target $result -Continue
92+
return
93+
}
9194
}
9295

9396
process {

functions/Remove-PdcDatabaseImage.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
function Remove-PdcDatabaseImage {
2+
[CmdLetBinding()]
23

34
param(
45
[parameter(Mandatory = $true)]
@@ -19,7 +20,13 @@ function Remove-PdcDatabaseImage {
1920
$ecDatabaseServer = Get-PSFConfigValue -FullName easyclone.database.server
2021

2122
# Test the module database setup
22-
Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
23+
$result = Test-PdcDatabaseSetup -SqlInstance $ecDatabaseServer -SqlCredential $SqlCredential -Database $ecDatabaseName
24+
25+
if(-not $result.Check){
26+
Stop-PSFFunction -Message $result.Message -Target $result -Continue
27+
return
28+
}
29+
2330
}
2431

2532
process {

functions/Set-PdcConfiguration.ps1

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ function Set-PdcConfiguration {
8080

8181
process {
8282

83+
# Test if there are any errors
84+
if (Test-PSFFunctionInterrupt) { return }
85+
8386
# Check if the database is already present
8487
if (($server.Databases.Name -contains $Database) -or ($server.Databases[$Database].Tables.Count -ge 1)) {
8588
if ($Force) {
@@ -116,26 +119,32 @@ function Set-PdcConfiguration {
116119
}
117120

118121
# Setup the path to the sql file
119-
$path = "$($MyInvocation.MyCommand.Module.ModuleBase)\internal\scripts\database.sql"
120-
$query = [System.IO.File]::ReadAllText($path)
122+
try {
123+
$path = "$($MyInvocation.MyCommand.Module.ModuleBase)\internal\scripts\database.sql"
124+
$query = [System.IO.File]::ReadAllText($path)
125+
}
126+
catch {
127+
Stop-PSFFunction -Message "Couldn't find database script. Make sure you have a valid installation of the module" -ErrorRecord $_ -Target $SqlInstance
128+
}
121129

130+
# Create the objects
122131
try {
123132
Write-PSFMessage -Message "Creating database objects" -Level Verbose
124133

125134
# Executing the query
126135
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database -Query $query
127136
}
128137
catch {
129-
Stop-PSFFunction -Message "Couldn't database objects" -ErrorRecord $_ -Target $SqlInstance
138+
Stop-PSFFunction -Message "Couldn't create database objects" -ErrorRecord $_ -Target $SqlInstance
130139
}
131140

132141
# Writing the setting to the configuration file
133142
Write-PSFMessage -Message "Registering config values" -Level Verbose
134-
Set-PSFConfig -Module EasyClone -Name database.server -Value $SqlInstance -Initialize -Validation string
135-
Set-PSFConfig -Module EasyClone -Name database.name -Value $Database -Initialize -Validation string
143+
Set-PSFConfig -Module PSDatabaseClone -Name database.server -Value $SqlInstance -Initialize -Validation string
144+
Set-PSFConfig -Module PSDatabaseClone -Name database.name -Value $Database -Initialize -Validation string
136145

137-
Get-PSFConfig -FullName easyclone.database.server | Register-PSFConfig -Scope SystemDefault
138-
Get-PSFConfig -FullName easyclone.database.name | Register-PSFConfig -Scope SystemDefault
146+
Get-PSFConfig -FullName psdatabaseclone.database.server | Register-PSFConfig -Scope SystemDefault
147+
Get-PSFConfig -FullName psdatabaseclone.database.name | Register-PSFConfig -Scope SystemDefault
139148
}
140149

141150
end {
Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,82 @@
11
function Test-PdcDatabaseSetup {
2+
3+
[CmdLetBinding()]
24
param(
3-
[Parameter(Mandatory=$true)]
4-
[ValidateNotNullOrEmpty()]
55
[Alias("ServerInstance", "SqlServerSqlServer")]
66
[object]$SqlInstance,
77

88
[System.Management.Automation.PSCredential]
99
$SqlCredential,
1010

11-
[Parameter(Mandatory=$true)]
12-
[ValidateNotNullOrEmpty()]
1311
[string]$Database
1412
)
1513

1614
Write-PSFMessage -Message "SqlInstance: $SqlInstance, Database: $Database" -Level Debug
1715

16+
# Create the info objects
17+
$result = [PSCustomObject]@{
18+
SqlInstance = $null
19+
Database = $null
20+
Check = $true
21+
Message = ""
22+
}
23+
24+
$errorOccured = $false
25+
1826
# Check if the values for the EasyClone database are set
19-
if (($SqlInstance -eq 'NotConfigured') -or ($Database -eq 'NotConfigured')) {
20-
Stop-PSFFunction -Message "The module is not yet configured. Please run Set-PdcConfiguration to make the neccesary changes" -Target $SqlInstance
21-
return
27+
if (($SqlInstance -eq $null) -or ($Database -eq $null)) {
28+
# Get the configurations for the program database
29+
$Database = Get-PSFConfigValue -FullName psdatabaseclone.database.name -Fallback "NotConfigured"
30+
$SqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server -Fallback "NotConfigured"
2231
}
2332

24-
Write-PSFMessage -Message "Attempting to connect to PSDatabaseClone database server $SqlInstance.." -Level Verbose
25-
try {
26-
$ecServer = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
33+
# Set the values in the info object
34+
$result.SqlInstance = $SqlInstance
35+
$result.Database = $Database
36+
37+
Write-PSFMessage -Message "Checking configurations" -Level Verbose
38+
39+
# Check the module database server and database name configurations
40+
if ($SqlInstance -eq 'NotConfigured') {
41+
$errorOccured = $true
42+
$result.Check = $false
43+
$result.Message = "The PSDatabaseClone database server is not yet configured. Please run Set-PdcConfiguration"
44+
45+
}
46+
47+
if ($Database -eq 'NotConfigured') {
48+
$errorOccured = $true
49+
$result.Check = $false
50+
$result.Message = "The PSDatabaseClone database is not yet configured. Please run Set-PdcConfiguration"
51+
2752
}
28-
catch {
29-
Stop-PSFFunction -Message "Could not connect to Sql Server instance $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
53+
54+
Write-PSFMessage -Message "Attempting to connect to PSDatabaseClone database server $SqlInstance.." -Level Verbose
55+
if ($result.Check) {
56+
try {
57+
$ecServer = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
58+
}
59+
catch {
60+
$errorOccured = $true
61+
$result.Check = $false
62+
$result.Message = "Could not connect to Sql Server instance $SqlInstance"
63+
}
3064
}
3165

3266
# Check if the easyclone database is present
3367
if ($ecServer.Databases.Name -notcontains $Database) {
34-
Stop-PSFFunction -Message "PSDatabaseClone database $Database is not present on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
68+
$errorOccured = $true
69+
$result.Check = $false
70+
$result.Message = "PSDatabaseClone database $Database is not present on $SqlInstance"
3571
}
72+
73+
# Check if an error occured
74+
if (-not $errorOccured) {
75+
$result.Message = "All OK"
76+
}
77+
78+
Write-PSFMessage -Message "Finished checking configurations" -Level Verbose
79+
80+
return $result
81+
3682
}

0 commit comments

Comments
 (0)