Skip to content

Commit 42b1bd5

Browse files
committed
Removed all hosts check. Added extra path checks. Made remove query more efficient
1 parent 484ac72 commit 42b1bd5

1 file changed

Lines changed: 18 additions & 26 deletions

File tree

functions/Remove-PSDCClone.ps1

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,6 @@
108108
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
109109
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
110110

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-
119111
Write-PSFMessage -Message "Started removing database clones" -Level Verbose
120112

121113
# Get all the items
@@ -164,7 +156,7 @@
164156

165157
# Loop through each of the results
166158
foreach ($item in $clone.Group) {
167-
159+
$item
168160
# Remove the database
169161
try {
170162
Write-PSFMessage -Message "Removing database $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose
@@ -177,42 +169,42 @@
177169

178170
# Dismounting the vhd
179171
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+
}
182176
}
183177
catch {
184178
Stop-PSFFunction -Message "Could not dismount vhd $($item.CloneLocation)" -ErrorRecord $_ -Target $result -Continue
185179
}
186180

187181
# Remove clone file and related access path
188182
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+
}
194192
}
195193
catch {
196194
Stop-PSFFunction -Message "Could not remove clone files" -ErrorRecord $_ -Target $result -Continue
197195
}
198196

199197
# Removing records from database
200198
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
210201
Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException
211202
}
212203
catch {
213204
Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue
214205
}
215-
} # end for each group item
206+
207+
} # End for each group item
216208

217209
} # End for each clone
218210

0 commit comments

Comments
 (0)