Skip to content

Commit f8d47dc

Browse files
committed
Made SqlInstance optional. Changed setup produre to only set what's needed. Implement unregistering configurations in begin block
1 parent f4e6281 commit f8d47dc

1 file changed

Lines changed: 73 additions & 66 deletions

File tree

functions/Set-PSDCConfiguration.ps1

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
#>
5656
[CmdLetBinding(SupportsShouldProcess = $true)]
5757
param(
58-
[parameter(Mandatory = $true)]
59-
[ValidateNotNullOrEmpty()]
6058
[object]$SqlInstance,
6159
[System.Management.Automation.PSCredential]
6260
$SqlCredential,
@@ -68,13 +66,15 @@
6866
Write-PSFMessage -Message "Started PSDatabaseClone Setup" -Level Output
6967

7068
# Try connecting to the instance
71-
Write-PSFMessage -Message "Attempting to connect to Sql Server $SqlInstance.." -Level Output
72-
try {
73-
$server = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
74-
}
75-
catch {
76-
Stop-PSFFunction -Message "Could not connect to Sql Server instance $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
77-
return
69+
if ($SqlInstance) {
70+
Write-PSFMessage -Message "Attempting to connect to Sql Server $SqlInstance.." -Level Output
71+
try {
72+
$server = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
73+
}
74+
catch {
75+
Stop-PSFFunction -Message "Could not connect to Sql Server instance $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
76+
return
77+
}
7878
}
7979

8080
# Setup the database name
@@ -84,6 +84,15 @@
8484

8585
# Set the flag for the new database
8686
[bool]$newDatabase = $false
87+
88+
# Unregister any configurations
89+
try{
90+
Unregister-PSFConfig -Scope SystemDefault -Module psdatabaseclone
91+
}
92+
catch{
93+
Stop-PSFFunction -Message "Something went wrong unregistering the configurations" -ErrorRecord $_ -Target $SqlInstance
94+
return
95+
}
8796
}
8897

8998
process {
@@ -92,87 +101,85 @@
92101
if (Test-PSFFunctionInterrupt) { return }
93102

94103
# Check if the database is already present
95-
if (($server.Databases.Name -contains $Database) -or ($server.Databases[$Database].Tables.Count -ge 1)) {
96-
if ($Force) {
97-
try {
98-
Remove-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database
104+
if ($SqlInstance) {
105+
if (($server.Databases.Name -contains $Database) -or ($server.Databases[$Database].Tables.Count -ge 1)) {
106+
if ($Force) {
107+
try {
108+
Remove-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database
109+
}
110+
catch {
111+
Stop-PSFFunction -Message "Couldn't remove database $Database on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
112+
return
113+
}
99114
}
100-
catch {
101-
Stop-PSFFunction -Message "Couldn't remove database $Database on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
102-
return
115+
else {
116+
Write-PSFMessage -Message "Database $Database already exists" -Level Verbose
103117
}
104118
}
105-
else {
106-
Write-PSFMessage -Message "Database $Database already exists" -Level Verbose
107-
}
108-
}
109119

110-
# Check if the database exists
111-
if ($server.Databases.Name -notcontains $Database) {
120+
# Check if the database exists
121+
if ($server.Databases.Name -notcontains $Database) {
112122

113-
# Set the flag
114-
$newDatabase = $true
123+
# Set the flag
124+
$newDatabase = $true
115125

116-
try {
117-
# Setup the query to create the database
118-
$query = "CREATE DATABASE [$Database]"
126+
try {
127+
# Setup the query to create the database
128+
$query = "CREATE DATABASE [$Database]"
119129

120-
Write-PSFMessage -Message "Creating database $Database on $SqlInstance" -Level Verbose
130+
Write-PSFMessage -Message "Creating database $Database on $SqlInstance" -Level Verbose
121131

122-
# Executing the query
123-
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database master -Query $query
132+
# Executing the query
133+
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database master -Query $query
134+
}
135+
catch {
136+
Stop-PSFFunction -Message "Couldn't create database $Database on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
137+
}
124138
}
125-
catch {
126-
Stop-PSFFunction -Message "Couldn't create database $Database on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
139+
else {
140+
# Check if there are any user objects already in the database
141+
$newDatabase = ($server.Databases[$Database].Tables.Count -eq 0)
127142
}
128-
}
129-
else{
130-
# Check if there are any user objects already in the database
131-
$newDatabase = ($server.Databases[$Database].Tables.Count -eq 0)
132-
}
133143

134-
# Setup the path to the sql file
135-
if ($newDatabase) {
136-
try {
137-
$path = "$($MyInvocation.MyCommand.Module.ModuleBase)\internal\scripts\database.sql"
138-
$query = [System.IO.File]::ReadAllText($path)
139-
140-
# Create the objects
144+
# Setup the path to the sql file
145+
if ($newDatabase) {
141146
try {
142-
Write-PSFMessage -Message "Creating database objects" -Level Verbose
143-
144-
# Executing the query
145-
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database -Query $query
147+
$path = "$($MyInvocation.MyCommand.Module.ModuleBase)\internal\scripts\database.sql"
148+
$query = [System.IO.File]::ReadAllText($path)
149+
150+
# Create the objects
151+
try {
152+
Write-PSFMessage -Message "Creating database objects" -Level Verbose
153+
154+
# Executing the query
155+
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database -Query $query
156+
}
157+
catch {
158+
Stop-PSFFunction -Message "Couldn't create database objects" -ErrorRecord $_ -Target $SqlInstance
159+
}
146160
}
147161
catch {
148-
Stop-PSFFunction -Message "Couldn't create database objects" -ErrorRecord $_ -Target $SqlInstance
162+
Stop-PSFFunction -Message "Couldn't find database script. Make sure you have a valid installation of the module" -ErrorRecord $_ -Target $SqlInstance
149163
}
150164
}
151-
catch {
152-
Stop-PSFFunction -Message "Couldn't find database script. Make sure you have a valid installation of the module" -ErrorRecord $_ -Target $SqlInstance
165+
else {
166+
Write-PSFMessage -Message "Database already contains objects" -Level Verbose
153167
}
154-
}
155-
else{
156-
Write-PSFMessage -Message "Database already contains objects" -Level Verbose
157-
}
158168

159-
# Writing the setting to the configuration file
160-
Write-PSFMessage -Message "Registering config values" -Level Verbose
161-
Set-PSFConfig -Module PSDatabaseClone -Name database.server -Value $SqlInstance -Initialize -Validation string
162-
Set-PSFConfig -Module PSDatabaseClone -Name database.name -Value $Database -Initialize -Validation string
169+
# Set the database server and database values
170+
Set-PSFConfig -Module PSDatabaseClone -Name database.server -Value $SqlInstance -Initialize -Validation string
171+
Set-PSFConfig -Module PSDatabaseClone -Name database.name -Value $Database -Initialize -Validation string
172+
}
163173

174+
# Set the credential for the database if needed
164175
if ($SqlCredential) {
165176
Set-PSFConfig -Module PSDatabaseClone -Name database.credential -Value $SqlCredential -Initialize
166177
}
167178

168-
if (Test-PSDCHyperVEnabled) {
169-
Set-PSFConfig -Module PSDatabaseClone -Name hyperv.enabled -Value $true -Initialize -Validation bool
170-
}
171-
else {
172-
Set-PSFConfig -Module PSDatabaseClone -Name hyperv.enabled -Value $false -Initialize -Validation bool
173-
}
174-
179+
# Set if Hyper-V is enabled
180+
Set-PSFConfig -Module PSDatabaseClone -Name hyperv.enabled -Value (Test-PSDCHyperVEnabled) -Validation bool
175181

182+
# Register the configurations in the system for all users
176183
Get-PSFConfig -FullName psdatabaseclone.database.server | Register-PSFConfig -Scope SystemDefault
177184
Get-PSFConfig -FullName psdatabaseclone.database.name | Register-PSFConfig -Scope SystemDefault
178185
Get-PSFConfig -FullName psdatabaseclone.database.credential | Register-PSFConfig -Scope SystemDefault

0 commit comments

Comments
 (0)