|
103 | 103 |
|
104 | 104 | begin { |
105 | 105 |
|
| 106 | + # Get the module configurations |
| 107 | + $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server |
| 108 | + $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
| 109 | + if (-not $PSDCSqlCredential) { |
| 110 | + $pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null |
| 111 | + } |
| 112 | + else { |
| 113 | + $pdcCredential = $PSDCSqlCredential |
| 114 | + } |
| 115 | + |
106 | 116 | # Test the module database setup |
107 | 117 | try { |
108 | | - Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException |
| 118 | + Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException |
109 | 119 | } |
110 | 120 | catch { |
111 | 121 | Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue |
112 | 122 | } |
113 | 123 |
|
114 | | - $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
115 | | - $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
116 | | - |
117 | 124 | Write-PSFMessage -Message "Started removing database clones" -Level Verbose |
118 | 125 |
|
119 | 126 | # Get all the items |
|
160 | 167 | Stop-PSFFunction -Message "Could not connect to Sql Server instance $($clone.Name)" -ErrorRecord $_ -Target $clone.Name -Continue |
161 | 168 | } |
162 | 169 |
|
| 170 | + # Setup the computer object |
| 171 | + $computer = [PsfComputer]$server.Name |
| 172 | + |
| 173 | + if (-not $computer.IsLocalhost) { |
| 174 | + $command = [scriptblock]::Create("Import-Module PSDatabaseClone") |
| 175 | + |
| 176 | + try { |
| 177 | + Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 178 | + } |
| 179 | + catch { |
| 180 | + Stop-PSFFunction -Message "Couldn't import module remotely" -Target $command |
| 181 | + return |
| 182 | + } |
| 183 | + } |
| 184 | + |
163 | 185 | # Loop through each of the results |
164 | 186 | foreach ($item in $clone.Group) { |
165 | 187 | $item |
|
177 | 199 | try { |
178 | 200 | if (Test-Path -Path $item.CloneLocation) { |
179 | 201 | Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose |
180 | | - $null = Dismount-VHD -Path $item.CloneLocation |
| 202 | + |
| 203 | + if ($computer.IsLocalhost) { |
| 204 | + $null = Dismount-VHD -Path $item.CloneLocation |
| 205 | + } |
| 206 | + else { |
| 207 | + $command = [scriptblock]::Create("Dismount-VHD -Path $($item.CloneLocation)") |
| 208 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 209 | + } |
181 | 210 | } |
182 | 211 | } |
183 | 212 | catch { |
|
186 | 215 |
|
187 | 216 | # Remove clone file and related access path |
188 | 217 | try { |
189 | | - if (Test-Path -Path $item.AccessPath) { |
190 | | - Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
191 | | - $null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force |
| 218 | + if ($computer.IsLocalhost) { |
| 219 | + if (Test-Path -Path $item.AccessPath) { |
| 220 | + Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
| 221 | + $null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force |
| 222 | + } |
| 223 | + |
| 224 | + if (Test-Path -Path $item.CloneLocation) { |
| 225 | + Write-PSFMessage -Message "Removing vhd" -Level Verbose |
| 226 | + $null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force |
| 227 | + } |
192 | 228 | } |
193 | | - |
194 | | - if (Test-Path -Path $item.CloneLocation) { |
195 | | - Write-PSFMessage -Message "Removing vhd" -Level Verbose |
196 | | - $null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force |
| 229 | + else { |
| 230 | + if (Test-Path -Path $item.AccessPath) { |
| 231 | + Write-PSFMessage -Message "Removing vhd access path" -Level Verbose |
| 232 | + $command = [scriptblock]::Create("Remove-Item -Path $($item.AccessPath) -Force") |
| 233 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 234 | + } |
| 235 | + |
| 236 | + if (Test-Path -Path $item.CloneLocation) { |
| 237 | + Write-PSFMessage -Message "Removing vhd" -Level Verbose |
| 238 | + $command = [scriptblock]::Create("Remove-Item -Path $($item.CloneLocation) -Force") |
| 239 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 240 | + } |
197 | 241 | } |
198 | 242 | } |
199 | 243 | catch { |
|
204 | 248 | try { |
205 | 249 | $query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);" |
206 | 250 |
|
207 | | - $null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException |
| 251 | + $null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query -EnableException |
208 | 252 | } |
209 | 253 | catch { |
210 | 254 | Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue |
|
0 commit comments