|
97 | 97 | $pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null |
98 | 98 | } |
99 | 99 | else { |
100 | | - $pdcCredential = $pdcCredential |
| 100 | + $pdcCredential = $PSDCSqlCredential |
101 | 101 | } |
102 | 102 |
|
103 | 103 | # Test the module database setup |
|
111 | 111 | Write-PSFMessage -Message "Started removing database images" -Level Verbose |
112 | 112 |
|
113 | 113 | # Get all the items |
114 | | - $items = Get-PSDCImage -pdcCredential $pdcCredential |
| 114 | + $items = Get-PSDCImage -PSDCSqlCredential $pdcCredential |
115 | 115 |
|
116 | 116 | if ($ImageID) { |
117 | 117 | Write-PSFMessage -Message "Filtering image ids" -Level Verbose |
|
154 | 154 | # Loop through each of the results |
155 | 155 | foreach ($item in $image.Group) { |
156 | 156 |
|
| 157 | + # Make up the data from the network path |
| 158 | + try { |
| 159 | + [uri]$uri = New-Object System.Uri($item.ImageLocation) |
| 160 | + $uriHost = $uri.Host |
| 161 | + } |
| 162 | + catch { |
| 163 | + Stop-PSFFunction -Message "The image location $ImageNetworkPath is not valid" -ErrorRecord $_ -Target $ImageNetworkPath |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + # Setup the computer object |
| 168 | + $computer = [PsfComputer]$uriHost |
| 169 | + |
157 | 170 | $query = " |
158 | | - SELECT c.CloneLocation, |
159 | | - c.AccessPath, |
160 | | - c.SqlInstance, |
161 | | - c.DatabaseName, |
162 | | - h.HostName, |
163 | | - h.IPAddress, |
164 | | - h.FQDN, |
165 | | - i.ImageLocation |
166 | | - FROM dbo.Image as i |
167 | | - INNER JOIN dbo.Clone AS c |
168 | | - ON c.ImageID = i.ImageID |
169 | | - INNER JOIN dbo.Host AS h |
170 | | - ON h.HostID = c.HostID |
171 | | - WHERE i.ImageID = $($item.ImageID) |
172 | | - ORDER BY h.HostName; |
173 | | - " |
| 171 | + SELECT c.CloneLocation, |
| 172 | + c.AccessPath, |
| 173 | + c.SqlInstance, |
| 174 | + c.DatabaseName, |
| 175 | + h.HostName, |
| 176 | + h.IPAddress, |
| 177 | + h.FQDN, |
| 178 | + i.ImageLocation |
| 179 | + FROM dbo.Image as i |
| 180 | + INNER JOIN dbo.Clone AS c |
| 181 | + ON c.ImageID = i.ImageID |
| 182 | + INNER JOIN dbo.Host AS h |
| 183 | + ON h.HostID = c.HostID |
| 184 | + WHERE i.ImageID = $($item.ImageID) |
| 185 | + ORDER BY h.HostName; |
| 186 | + " |
174 | 187 |
|
175 | 188 | # Try to get the neccesary info from the EasyClone database |
176 | 189 | try { |
|
187 | 200 | # Remove the clones for the host |
188 | 201 | try { |
189 | 202 | Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose |
190 | | - Remove-PSDCClone -HostName $result.HostName -Database $result.DatabaseName -pdcCredential $pdcCredential -Credential $Credential -Confirm:$false |
| 203 | + Remove-PSDCClone -HostName $result.HostName -Database $result.DatabaseName -PSDCSqlCredential $pdcCredential -Credential $Credential -Confirm:$false |
191 | 204 | } |
192 | 205 | catch { |
193 | 206 | Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue |
|
204 | 217 |
|
205 | 218 | # Remove the image from the file system |
206 | 219 | try { |
207 | | - if (Test-Path -Path $item.ImageLocation -Credential $Credential) { |
208 | | - Write-PSFMessage -Message "Removing image '$($item.ImageLocation)' from file system" -Level Verbose |
209 | | - $null = Remove-Item -Path $item.ImageLocation -Credential $Credential -Force:$Force |
| 220 | + if ($computer.IsLocalhost) { |
| 221 | + if (Test-Path -Path $item.ImageLocation -Credential $Credential) { |
| 222 | + Write-PSFMessage -Message "Removing image '$($item.ImageLocation)' from file system" -Level Verbose |
| 223 | + |
| 224 | + $null = Remove-Item -Path $item.ImageLocation -Credential $Credential -Force:$Force |
| 225 | + } |
| 226 | + else { |
| 227 | + Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose |
| 228 | + } |
210 | 229 | } |
211 | 230 | else { |
212 | | - Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose |
| 231 | + $command = [scriptblock]::Create("Test-Path -Path $($item.ImageLocation)") |
| 232 | + $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 233 | + |
| 234 | + if ($result) { |
| 235 | + $command = [scriptblock]::Create("Remove-Item -Path $($item.ImageLocation) -Force") |
| 236 | + $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 237 | + } |
| 238 | + else { |
| 239 | + Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose |
| 240 | + } |
213 | 241 | } |
214 | 242 | } |
215 | 243 | catch { |
|
0 commit comments