|
55 | 55 | #> |
56 | 56 | [CmdLetBinding(SupportsShouldProcess = $true)] |
57 | 57 | param( |
58 | | - [parameter(Mandatory = $true)] |
59 | | - [ValidateNotNullOrEmpty()] |
60 | 58 | [object]$SqlInstance, |
61 | 59 | [System.Management.Automation.PSCredential] |
62 | 60 | $SqlCredential, |
|
68 | 66 | Write-PSFMessage -Message "Started PSDatabaseClone Setup" -Level Output |
69 | 67 |
|
70 | 68 | # 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 | + } |
78 | 78 | } |
79 | 79 |
|
80 | 80 | # Setup the database name |
|
84 | 84 |
|
85 | 85 | # Set the flag for the new database |
86 | 86 | [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 | + } |
87 | 96 | } |
88 | 97 |
|
89 | 98 | process { |
|
92 | 101 | if (Test-PSFFunctionInterrupt) { return } |
93 | 102 |
|
94 | 103 | # 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 | + } |
99 | 114 | } |
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 |
103 | 117 | } |
104 | 118 | } |
105 | | - else { |
106 | | - Write-PSFMessage -Message "Database $Database already exists" -Level Verbose |
107 | | - } |
108 | | - } |
109 | 119 |
|
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) { |
112 | 122 |
|
113 | | - # Set the flag |
114 | | - $newDatabase = $true |
| 123 | + # Set the flag |
| 124 | + $newDatabase = $true |
115 | 125 |
|
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]" |
119 | 129 |
|
120 | | - Write-PSFMessage -Message "Creating database $Database on $SqlInstance" -Level Verbose |
| 130 | + Write-PSFMessage -Message "Creating database $Database on $SqlInstance" -Level Verbose |
121 | 131 |
|
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 | + } |
124 | 138 | } |
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) |
127 | 142 | } |
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 | | - } |
133 | 143 |
|
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) { |
141 | 146 | 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 | + } |
146 | 160 | } |
147 | 161 | 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 |
149 | 163 | } |
150 | 164 | } |
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 |
153 | 167 | } |
154 | | - } |
155 | | - else{ |
156 | | - Write-PSFMessage -Message "Database already contains objects" -Level Verbose |
157 | | - } |
158 | 168 |
|
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 | + } |
163 | 173 |
|
| 174 | + # Set the credential for the database if needed |
164 | 175 | if ($SqlCredential) { |
165 | 176 | Set-PSFConfig -Module PSDatabaseClone -Name database.credential -Value $SqlCredential -Initialize |
166 | 177 | } |
167 | 178 |
|
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 |
175 | 181 |
|
| 182 | + # Register the configurations in the system for all users |
176 | 183 | Get-PSFConfig -FullName psdatabaseclone.database.server | Register-PSFConfig -Scope SystemDefault |
177 | 184 | Get-PSFConfig -FullName psdatabaseclone.database.name | Register-PSFConfig -Scope SystemDefault |
178 | 185 | Get-PSFConfig -FullName psdatabaseclone.database.credential | Register-PSFConfig -Scope SystemDefault |
|
0 commit comments