Skip to content

Commit a8d447e

Browse files
committed
Changed check in Test-PdcConfiguration
Fixed Remove-PdcDatabaseImage to also remove images if no clone was made with it
1 parent e4d1974 commit a8d447e

2 files changed

Lines changed: 32 additions & 22 deletions

File tree

functions/Remove-PdcDatabaseImage.ps1

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function Remove-PdcDatabaseImage {
2-
<#
2+
<#
33
.SYNOPSIS
44
Remove-PdcDatabaseImage removes one or more images
55
@@ -48,11 +48,14 @@ function Remove-PdcDatabaseImage {
4848
# Test the module database setup
4949
$result = Test-PdcConfiguration
5050

51-
if(-not $result.Check){
51+
if (-not $result.Check) {
5252
Stop-PSFFunction -Message $result.Message -Target $result -Continue
5353
return
5454
}
5555

56+
# Get the database values
57+
$pdcDatabaseServer = $result.SqlInstance
58+
$pdcDatabaseName = $result.Database
5659
}
5760

5861
process {
@@ -71,11 +74,11 @@ function Remove-PdcDatabaseImage {
7174
h.IPAddress,
7275
h.FQDN,
7376
i.ImageLocation
74-
FROM dbo.Clone AS c
77+
FROM dbo.Image as i
78+
INNER JOIN dbo.Clone AS c
79+
ON c.ImageID = i.ImageID
7580
INNER JOIN dbo.Host AS h
7681
ON h.HostID = c.HostID
77-
INNER JOIN dbo.Image AS i
78-
ON i.ImageID = c.ImageID
7982
WHERE i.ImageLocation = '$image'
8083
ORDER BY h.HostName;
8184
"
@@ -97,35 +100,42 @@ function Remove-PdcDatabaseImage {
97100

98101
# Remove the clones for the host
99102
try {
100-
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
101-
Remove-PdcDatabaseClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential
103+
if ($result.HostName -ne $null) {
104+
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
105+
Remove-PdcDatabaseClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential
106+
}
102107
}
103108
catch {
104109
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -Target $result -Continue
105110
}
106111

107112
}
113+
}
114+
else {
115+
Write-PSFMessage -Message "No clones were found created with image $image" -Level Verbose
116+
}
108117

109-
# Remove the image from the file system
110-
try {
118+
# Remove the image from the file system
119+
try {
120+
if (Test-Path -Path $image -Credential $Credential) {
111121
Write-PSFMessage -Message "Removing image '$image' from file system" -Level Verbose
112122
$null = Remove-Item -Path $image -Credential $Credential -Force:$Force
113123
}
114-
catch {
115-
Stop-PSFFunction -Message "Couldn't remove image '$image' from file system" -Target $result
124+
else{
125+
Write-PSFMessage -Message "Couldn't find image $image" -Level Verbose
116126
}
127+
}
128+
catch {
129+
Stop-PSFFunction -Message "Couldn't remove image '$image' from file system" -Target $result
130+
}
117131

118-
# Remove the image from the database
119-
try {
120-
$query = "DELETE FROM dbo.Image WHERE ImageLocation = '$image'"
121-
$results = Invoke-DbaSqlQuery -SqlInstance $ecDatabaseServer -Database $ecDatabaseName -Query $query
122-
}
123-
catch {
124-
Stop-PSFFunction -Message "Couldn't remove image '$image' from database" -Target $result
125-
}
132+
# Remove the image from the database
133+
try {
134+
$query = "DELETE FROM dbo.Image WHERE ImageLocation = '$image'"
135+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcDatabaseServer -Database $pdcDatabaseName -Query $query
126136
}
127-
else {
128-
Write-PSFMessage -Message "No clones were found created with image $image" -Level Verbose
137+
catch {
138+
Stop-PSFFunction -Message "Couldn't remove image '$image' from database" -Target $result
129139
}
130140

131141
} # End for each image

internal/functions/Test-PdcConfiguration.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function Test-PdcConfiguration {
8484
}
8585

8686
Write-PSFMessage -Message "Attempting to connect to PSDatabaseClone database server $SqlInstance.." -Level Verbose
87-
if ($result.Check) {
87+
if (-not $errorOccured) {
8888
try {
8989
$ecServer = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
9090
}

0 commit comments

Comments
 (0)