|
108 | 108 | $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
109 | 109 | $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
110 | 110 |
|
111 | | - if (-not $HostName -and -not $InputObject) { |
112 | | - |
113 | | - if(-not $PSCmdlet.ShouldProcess("All hosts" , "Delete all clones on all hosts?`nIf not please say no and use -HostName and/or -Database")){ |
114 | | - return |
115 | | - } |
116 | | - |
117 | | - } |
118 | | - |
119 | 111 | Write-PSFMessage -Message "Started removing database clones" -Level Verbose |
120 | 112 |
|
121 | 113 | # Get all the items |
|
164 | 156 |
|
165 | 157 | # Loop through each of the results |
166 | 158 | foreach ($item in $clone.Group) { |
167 | | - |
| 159 | + $item |
168 | 160 | # Remove the database |
169 | 161 | try { |
170 | 162 | Write-PSFMessage -Message "Removing database $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose |
|
177 | 169 |
|
178 | 170 | # Dismounting the vhd |
179 | 171 | try { |
180 | | - Write-PSFMessage -Message "Dismounting disk $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose |
181 | | - $null = Dismount-VHD -Path $item.CloneLocation |
| 172 | + if (Test-Path -Path $item.CloneLocation) { |
| 173 | + Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose |
| 174 | + $null = Dismount-VHD -Path $item.CloneLocation |
| 175 | + } |
182 | 176 | } |
183 | 177 | catch { |
184 | 178 | Stop-PSFFunction -Message "Could not dismount vhd $($item.CloneLocation)" -ErrorRecord $_ -Target $result -Continue |
185 | 179 | } |
186 | 180 |
|
187 | 181 | # Remove clone file and related access path |
188 | 182 | try { |
189 | | - Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
190 | | - $null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force |
191 | | - |
192 | | - Write-PSFMessage -Message "Removing vhd" -Level Verbose |
193 | | - $null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force |
| 183 | + if (Test-Path -Path $item.AccessPath) { |
| 184 | + Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
| 185 | + $null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force |
| 186 | + } |
| 187 | + |
| 188 | + if (Test-Path -Path $item.CloneLocation) { |
| 189 | + Write-PSFMessage -Message "Removing vhd" -Level Verbose |
| 190 | + $null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force |
| 191 | + } |
194 | 192 | } |
195 | 193 | catch { |
196 | 194 | Stop-PSFFunction -Message "Could not remove clone files" -ErrorRecord $_ -Target $result -Continue |
197 | 195 | } |
198 | 196 |
|
199 | 197 | # Removing records from database |
200 | 198 | try { |
201 | | - $query = " |
202 | | - DELETE c |
203 | | - FROM dbo.Clone AS c |
204 | | - INNER JOIN dbo.Host AS h |
205 | | - ON h.HostID = h.HostID |
206 | | - WHERE h.HostName = '$($item.HostName)' |
207 | | - AND c.CloneLocation = '$($item.CloneLocation)'; |
208 | | - " |
209 | | - |
| 199 | + $query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);" |
| 200 | + Write-PSFMessage -Message $query -Level Verbose |
210 | 201 | Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException |
211 | 202 | } |
212 | 203 | catch { |
213 | 204 | Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue |
214 | 205 | } |
215 | | - } # end for each group item |
| 206 | + |
| 207 | + } # End for each group item |
216 | 208 |
|
217 | 209 | } # End for each clone |
218 | 210 |
|
|
0 commit comments