Skip to content

Commit d550136

Browse files
committed
Added pipeline support
1 parent 0999890 commit d550136

1 file changed

Lines changed: 113 additions & 52 deletions

File tree

functions/Remove-PDCImage.ps1

Lines changed: 113 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
The command will remove an image from PSDatabaseClone.
88
It will also remove all the clones associated with it on the hosts.
99
10+
.PARAMETER ImageID
11+
Remove images based on the image id
12+
13+
.PARAMETER ImageName
14+
Remove images based on the image name
15+
1016
.PARAMETER ImageLocation
1117
Location of the image as it's saved in the database or can be seen on the file system.
1218
19+
.PARAMETER Database
20+
Remove images based on the database
21+
22+
.PARAMETER ExcludeDatabase
23+
Filter the images based on the excluded database
24+
1325
.PARAMETER Credential
1426
Allows you to login to servers using Windows Auth/Integrated/Trusted. To use:
1527
@@ -18,6 +30,9 @@
1830
.PARAMETER Force
1931
Forcefully remove the items.
2032
33+
.PARAMETER InputObject
34+
The input object that is used for pipeline use
35+
2136
.PARAMETER EnableException
2237
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
2338
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
@@ -44,15 +59,21 @@
4459
4560
Remove an image
4661
#>
47-
[CmdLetBinding(SupportsShouldProcess = $true)]
62+
[CmdLetBinding(DefaultParameterSetName = "ImageLocation", SupportsShouldProcess = $true,
63+
ConfirmImpact = 'High')]
4864

4965
param(
50-
[parameter(Mandatory = $true)]
51-
[ValidateNotNullOrEmpty()]
66+
[int[]]$ImageID,
67+
[string[]]$ImageName,
68+
[parameter(ParameterSetName = "ImageLocation")]
5269
[string[]]$ImageLocation,
70+
[string[]]$Database,
71+
[string[]]$ExcludeDatabase,
5372
[System.Management.Automation.PSCredential]
5473
$Credential,
5574
[switch]$Force,
75+
[parameter(ValueFromPipeline = $true, ParameterSetName = "Image")]
76+
[object[]]$InputObject,
5677
[switch]$EnableException
5778
)
5879

@@ -71,15 +92,52 @@
7192
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
7293

7394
Write-PSFMessage -Message "Started removing database images" -Level Verbose
95+
96+
# Get all the items
97+
$items = Get-PDCImage
98+
99+
if ($ImageID) {
100+
Write-PSFMessage -Message "Filtering image ids" -Level Verbose
101+
$items = $items | Where-Object {$_.ImageID -in $ImageID}
102+
}
103+
104+
if ($ImageName) {
105+
Write-PSFMessage -Message "Filtering image name" -Level Verbose
106+
$items = $items | Where-Object {$_.ImageName -in $ImageName}
107+
}
108+
109+
if ($ImageLocation) {
110+
Write-PSFMessage -Message "Filtering image locations" -Level Verbose
111+
$items = $items | Where-Object {$_.ImageLocation -in $ImageLocation}
112+
}
113+
114+
if ($Database) {
115+
Write-PSFMessage -Message "Filtering databases" -Level Verbose
116+
$items = $items | Where-Object {$_.DatabaseName -in $Database}
117+
}
118+
119+
if ($ExcludeDatabase) {
120+
Write-PSFMessage -Message "Filtering excluded databases" -Level Verbose
121+
$items = $items | Where-Object {$_.DatabaseName -notin $Database}
122+
}
123+
124+
# Append the items
125+
$InputObject += $items
74126
}
75127

76128
process {
77129
# Test if there are any errors
78130
if (Test-PSFFunctionInterrupt) { return }
79131

80-
foreach ($image in $ImageLocation) {
132+
# Group the objects to make it easier to go through
133+
$images = $InputObject | Group-Object ImageID
134+
135+
foreach ($image in $images) {
81136

82-
$query = "
137+
# Loop through each of the results
138+
foreach ($item in $image.Group) {
139+
140+
$query = "
83141
SELECT c.CloneLocation,
84142
c.AccessPath,
85143
c.SqlInstance,
@@ -93,62 +151,65 @@
93151
ON c.ImageID = i.ImageID
94152
INNER JOIN dbo.Host AS h
95153
ON h.HostID = c.HostID
96-
WHERE i.ImageLocation = '$image'
154+
WHERE i.ImageID = $($item.ImageID)
97155
ORDER BY h.HostName;
98156
"
99157

100-
# Try to get the neccesary info from the EasyClone database
101-
try {
102-
Write-PSFMessage -Message "Retrieving data for image '$image'" -Level Verbose
103-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject
104-
105-
# Check the results
106-
if ($results.Count -ge 1) {
107-
108-
# Loop through the results
109-
foreach ($result in $results) {
110-
111-
# Remove the clones for the host
112-
try {
113-
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
114-
Remove-PDCClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential -Confirm:$false
115-
}
116-
catch {
117-
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
158+
# Try to get the neccesary info from the EasyClone database
159+
try {
160+
Write-PSFMessage -Message "Retrieving data for image '$($item.Name)'" -Level Verbose
161+
$results = @()
162+
$results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
163+
164+
# Check the results
165+
if ($results.Count -ge 1) {
166+
167+
# Loop through the results
168+
foreach ($result in $results) {
169+
170+
# Remove the clones for the host
171+
try {
172+
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
173+
Remove-PDCClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential -Confirm:$false
174+
}
175+
catch {
176+
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
177+
}
118178
}
119179
}
180+
else {
181+
Write-PSFMessage -Message "No clones were found created with image $($image.Name)" -Level Verbose
182+
}
120183
}
121-
else {
122-
Write-PSFMessage -Message "No clones were found created with image $image" -Level Verbose
184+
catch {
185+
Stop-PSFFunction -Message "Couldn't retrieve clone records for host $($result.HostName)" -ErrorRecord $_ -Target $hst -Continue
123186
}
124-
}
125-
catch {
126-
Stop-PSFFunction -Message "Couldn't retrieve clone records for host $hst" -ErrorRecord $_ -Target $hst -Continue
127-
}
128-
129-
# Remove the image from the file system
130-
try {
131-
if (Test-Path -Path $image -Credential $Credential) {
132-
Write-PSFMessage -Message "Removing image '$image' from file system" -Level Verbose
133-
$null = Remove-Item -Path $image -Credential $Credential -Force:$Force
187+
188+
# Remove the image from the file system
189+
try {
190+
if (Test-Path -Path $item.ImageLocation -Credential $Credential) {
191+
Write-PSFMessage -Message "Removing image '$($item.ImageLocation)' from file system" -Level Verbose
192+
$null = Remove-Item -Path $item.ImageLocation -Credential $Credential -Force:$Force
193+
}
194+
else {
195+
Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose
196+
}
197+
}
198+
catch {
199+
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from file system" -ErrorRecord $_ -Target $result
134200
}
135-
else {
136-
Write-PSFMessage -Message "Couldn't find image $image" -Level Verbose
201+
202+
# Remove the image from the database
203+
try {
204+
$query = "DELETE FROM dbo.Image WHERE ImageID = $($item.ImageID)"
205+
206+
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
137207
}
138-
}
139-
catch {
140-
Stop-PSFFunction -Message "Couldn't remove image '$image' from file system" -ErrorRecord $_ -Target $result
141-
}
142-
143-
# Remove the image from the database
144-
try {
145-
$query = "DELETE FROM dbo.Image WHERE ImageLocation = '$image'"
146-
147-
$null = Invoke-DbaSqlQuery -SqlInstance $pdcDatabaseServer -Database $pdcDatabaseName -Query $query
148-
}
149-
catch {
150-
Stop-PSFFunction -Message "Couldn't remove image '$image' from database" -ErrorRecord $_ -Target $result
151-
}
208+
catch {
209+
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from database" -ErrorRecord $_ -Target $query
210+
}
211+
212+
} # End for each item in group
152213

153214
} # End for each image
154215

0 commit comments

Comments
 (0)