|
184 | 184 |
|
185 | 185 | # Loop through each of the results |
186 | 186 | foreach ($item in $clone.Group) { |
187 | | - $item |
188 | | - # Remove the database |
189 | | - try { |
190 | | - Write-PSFMessage -Message "Removing database $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose |
191 | | - |
192 | | - $null = Remove-DbaDatabase -SqlInstance $item.SqlInstance -SqlCredential $SqlCredential -Database $item.DatabaseName -Confirm:$false -EnableException |
193 | | - } |
194 | | - catch { |
195 | | - Stop-PSFFunction -Message "Could not remove database $($item.DatabaseName) from $server" -ErrorRecord $_ -Target $server -Continue |
196 | | - } |
197 | 187 |
|
198 | | - # Dismounting the vhd |
199 | | - try { |
| 188 | + if ($PSCmdlet.ShouldProcess($item.DatabaseName, "Removing database $($item.DatabaseName)")) { |
| 189 | + # Remove the database |
| 190 | + try { |
| 191 | + Write-PSFMessage -Message "Removing database $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose |
200 | 192 |
|
201 | | - if ($computer.IsLocalhost) { |
202 | | - $null = Dismount-VHD -Path $item.CloneLocation -Credential $Credential |
| 193 | + $null = Remove-DbaDatabase -SqlInstance $item.SqlInstance -SqlCredential $SqlCredential -Database $item.DatabaseName -Confirm:$false -EnableException |
203 | 194 | } |
204 | | - else { |
205 | | - $command = [ScriptBlock]::Create("Test-Path -Path $($item.CloneLocation)") |
206 | | - Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose |
207 | | - $result = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
208 | | - #if (-not $result) { |
209 | | - $command = [scriptblock]::Create("Dismount-VHD -Path `"$($item.CloneLocation)`"") |
210 | | - $null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
211 | | - #} |
| 195 | + catch { |
| 196 | + Stop-PSFFunction -Message "Could not remove database $($item.DatabaseName) from $server" -ErrorRecord $_ -Target $server -Continue |
212 | 197 | } |
213 | 198 | } |
214 | | - catch { |
215 | | - Stop-PSFFunction -Message "Could not dismount vhd $($item.CloneLocation)" -ErrorRecord $_ -Target $result -Continue |
216 | | - } |
217 | | - |
218 | | - # Remove clone file and related access path |
219 | | - try { |
220 | | - if ($computer.IsLocalhost) { |
221 | | - Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
222 | | - $null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force |
223 | 199 |
|
224 | | - Write-PSFMessage -Message "Removing vhd" -Level Verbose |
225 | | - $null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force |
| 200 | + if ($PSCmdlet.ShouldProcess($item.CloneLocation, "Dismounting the vhd")) { |
| 201 | + # Dismounting the vhd |
| 202 | + try { |
| 203 | + |
| 204 | + if ($computer.IsLocalhost) { |
| 205 | + $null = Dismount-VHD -Path $item.CloneLocation -Credential $Credential |
| 206 | + } |
| 207 | + else { |
| 208 | + $command = [ScriptBlock]::Create("Test-Path -Path $($item.CloneLocation)") |
| 209 | + Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose |
| 210 | + $result = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
| 211 | + #if (-not $result) { |
| 212 | + $command = [scriptblock]::Create("Dismount-VHD -Path `"$($item.CloneLocation)`"") |
| 213 | + $null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
| 214 | + #} |
| 215 | + } |
226 | 216 | } |
227 | | - else { |
228 | | - Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
229 | | - $command = [scriptblock]::Create("Remove-Item -Path $($item.AccessPath) -Force") |
230 | | - $null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
231 | | - |
232 | | - Write-PSFMessage -Message "Removing vhd" -Level Verbose |
233 | | - $command = [scriptblock]::Create("Remove-Item -Path $($item.CloneLocation) -Force") |
234 | | - $null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
| 217 | + catch { |
| 218 | + Stop-PSFFunction -Message "Could not dismount vhd $($item.CloneLocation)" -ErrorRecord $_ -Target $result -Continue |
235 | 219 | } |
236 | 220 | } |
237 | | - catch { |
238 | | - Stop-PSFFunction -Message "Could not remove clone files" -ErrorRecord $_ -Target $result -Continue |
| 221 | + |
| 222 | + if ($PSCmdlet.ShouldProcess($item.CloneLocation, "Removing clone files and folders")) { |
| 223 | + # Remove clone file and related access path |
| 224 | + try { |
| 225 | + if ($computer.IsLocalhost) { |
| 226 | + Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
| 227 | + $null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force |
| 228 | + |
| 229 | + Write-PSFMessage -Message "Removing vhd" -Level Verbose |
| 230 | + $null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force |
| 231 | + } |
| 232 | + else { |
| 233 | + Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
| 234 | + $command = [scriptblock]::Create("Remove-Item -Path $($item.AccessPath) -Force") |
| 235 | + $null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
| 236 | + |
| 237 | + Write-PSFMessage -Message "Removing vhd" -Level Verbose |
| 238 | + $command = [scriptblock]::Create("Remove-Item -Path $($item.CloneLocation) -Force") |
| 239 | + $null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential |
| 240 | + } |
| 241 | + } |
| 242 | + catch { |
| 243 | + Stop-PSFFunction -Message "Could not remove clone files" -ErrorRecord $_ -Target $result -Continue |
| 244 | + } |
239 | 245 | } |
240 | 246 |
|
241 | | - # Removing records from database |
242 | | - try { |
243 | | - $query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);" |
| 247 | + if ($PSCmdlet.ShouldProcess($item.CloneID, "Deleting clone rom database")) { |
| 248 | + # Removing records from database |
| 249 | + try { |
| 250 | + $query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);" |
244 | 251 |
|
245 | | - $null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query -EnableException |
246 | | - } |
247 | | - catch { |
248 | | - Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue |
| 252 | + $null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query -EnableException |
| 253 | + } |
| 254 | + catch { |
| 255 | + Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue |
| 256 | + } |
249 | 257 | } |
250 | 258 |
|
251 | 259 | } # End for each group item |
|
0 commit comments