Skip to content

Commit dc29f52

Browse files
committed
Removed array
cleaned yp try/catch blocks
1 parent fb54cf9 commit dc29f52

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

functions/Remove-PDCImage.ps1

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function Remove-PDCImage {
2-
<#
2+
<#
33
.SYNOPSIS
44
Remove-PDCImage removes one or more images
55
@@ -63,7 +63,6 @@ function Remove-PDCImage {
6363
if (Test-PSFFunctionInterrupt) { return }
6464

6565
foreach ($image in $ImageLocation) {
66-
[array]$results = $null
6766

6867
$query = "
6968
SELECT c.CloneLocation,
@@ -86,33 +85,30 @@ function Remove-PDCImage {
8685
# Try to get the neccesary info from the EasyClone database
8786
try {
8887
Write-PSFMessage -Message "Retrieving data for image '$image'" -Level Verbose
89-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
90-
}
91-
catch {
92-
Stop-PSFFunction -Message "Couldn't retrieve clone records for host $hst" -Target $hst -Continue
93-
}
88+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject
9489

95-
# Check the results
96-
if ($results.Count -ge 1) {
90+
# Check the results
91+
if ($results.Count -ge 1) {
9792

98-
# Loop through the results
99-
foreach ($result in $results) {
93+
# Loop through the results
94+
foreach ($result in $results) {
10095

101-
# Remove the clones for the host
102-
try {
103-
if ($result.HostName -ne $null) {
96+
# Remove the clones for the host
97+
try {
10498
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
105-
Remove-PDCClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential
99+
Remove-PDCClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential -Confirm:$false
100+
}
101+
catch {
102+
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
106103
}
107104
}
108-
catch {
109-
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -Target $result -Continue
110-
}
111-
105+
}
106+
else {
107+
Write-PSFMessage -Message "No clones were found created with image $image" -Level Verbose
112108
}
113109
}
114-
else {
115-
Write-PSFMessage -Message "No clones were found created with image $image" -Level Verbose
110+
catch {
111+
Stop-PSFFunction -Message "Couldn't retrieve clone records for host $hst" -ErrorRecord $_ -Target $hst -Continue
116112
}
117113

118114
# Remove the image from the file system
@@ -126,16 +122,17 @@ function Remove-PDCImage {
126122
}
127123
}
128124
catch {
129-
Stop-PSFFunction -Message "Couldn't remove image '$image' from file system" -Target $result
125+
Stop-PSFFunction -Message "Couldn't remove image '$image' from file system" -ErrorRecord $_ -Target $result
130126
}
131127

132128
# Remove the image from the database
133129
try {
134130
$query = "DELETE FROM dbo.Image WHERE ImageLocation = '$image'"
135-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcDatabaseServer -Database $pdcDatabaseName -Query $query
131+
132+
$null = Invoke-DbaSqlQuery -SqlInstance $pdcDatabaseServer -Database $pdcDatabaseName -Query $query
136133
}
137134
catch {
138-
Stop-PSFFunction -Message "Couldn't remove image '$image' from database" -Target $result
135+
Stop-PSFFunction -Message "Couldn't remove image '$image' from database" -ErrorRecord $_ -Target $result
139136
}
140137

141138
} # End for each image

0 commit comments

Comments
 (0)