|
7 | 7 | The command will remove an image from PSDatabaseClone. |
8 | 8 | It will also remove all the clones associated with it on the hosts. |
9 | 9 |
|
| 10 | +.PARAMETER ImageID |
| 11 | + Remove images based on the image id |
| 12 | +
|
| 13 | +.PARAMETER ImageName |
| 14 | + Remove images based on the image name |
| 15 | +
|
10 | 16 | .PARAMETER ImageLocation |
11 | 17 | Location of the image as it's saved in the database or can be seen on the file system. |
12 | 18 |
|
| 19 | +.PARAMETER Database |
| 20 | + Remove images based on the database |
| 21 | +
|
| 22 | +.PARAMETER ExcludeDatabase |
| 23 | + Filter the images based on the excluded database |
| 24 | +
|
13 | 25 | .PARAMETER Credential |
14 | 26 | Allows you to login to servers using Windows Auth/Integrated/Trusted. To use: |
15 | 27 |
|
|
18 | 30 | .PARAMETER Force |
19 | 31 | Forcefully remove the items. |
20 | 32 |
|
| 33 | +.PARAMETER InputObject |
| 34 | + The input object that is used for pipeline use |
| 35 | +
|
21 | 36 | .PARAMETER EnableException |
22 | 37 | By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. |
23 | 38 | This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. |
|
44 | 59 |
|
45 | 60 | Remove an image |
46 | 61 | #> |
47 | | - [CmdLetBinding(SupportsShouldProcess = $true)] |
| 62 | + [CmdLetBinding(DefaultParameterSetName = "ImageLocation", SupportsShouldProcess = $true, |
| 63 | + ConfirmImpact = 'High')] |
48 | 64 |
|
49 | 65 | param( |
50 | | - [parameter(Mandatory = $true)] |
51 | | - [ValidateNotNullOrEmpty()] |
| 66 | + [int[]]$ImageID, |
| 67 | + [string[]]$ImageName, |
| 68 | + [parameter(ParameterSetName = "ImageLocation")] |
52 | 69 | [string[]]$ImageLocation, |
| 70 | + [string[]]$Database, |
| 71 | + [string[]]$ExcludeDatabase, |
53 | 72 | [System.Management.Automation.PSCredential] |
54 | 73 | $Credential, |
55 | 74 | [switch]$Force, |
| 75 | + [parameter(ValueFromPipeline = $true, ParameterSetName = "Image")] |
| 76 | + [object[]]$InputObject, |
56 | 77 | [switch]$EnableException |
57 | 78 | ) |
58 | 79 |
|
|
71 | 92 | $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
72 | 93 |
|
73 | 94 | 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 |
74 | 126 | } |
75 | 127 |
|
76 | 128 | process { |
77 | 129 | # Test if there are any errors |
78 | 130 | if (Test-PSFFunctionInterrupt) { return } |
79 | 131 |
|
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) { |
81 | 136 |
|
82 | | - $query = " |
| 137 | + # Loop through each of the results |
| 138 | + foreach ($item in $image.Group) { |
| 139 | + |
| 140 | + $query = " |
83 | 141 | SELECT c.CloneLocation, |
84 | 142 | c.AccessPath, |
85 | 143 | c.SqlInstance, |
|
93 | 151 | ON c.ImageID = i.ImageID |
94 | 152 | INNER JOIN dbo.Host AS h |
95 | 153 | ON h.HostID = c.HostID |
96 | | - WHERE i.ImageLocation = '$image' |
| 154 | + WHERE i.ImageID = $($item.ImageID) |
97 | 155 | ORDER BY h.HostName; |
98 | 156 | " |
99 | 157 |
|
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 | + } |
118 | 178 | } |
119 | 179 | } |
| 180 | + else { |
| 181 | + Write-PSFMessage -Message "No clones were found created with image $($image.Name)" -Level Verbose |
| 182 | + } |
120 | 183 | } |
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 |
123 | 186 | } |
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 |
134 | 200 | } |
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 |
137 | 207 | } |
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 |
152 | 213 |
|
153 | 214 | } # End for each image |
154 | 215 |
|
|
0 commit comments