Skip to content

Commit 1621455

Browse files
committed
Added remote support
1 parent 49a0871 commit 1621455

1 file changed

Lines changed: 57 additions & 13 deletions

File tree

functions/Remove-PSDCClone.ps1

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,24 @@
103103

104104
begin {
105105

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+
106116
# Test the module database setup
107117
try {
108-
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
118+
Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException
109119
}
110120
catch {
111121
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
112122
}
113123

114-
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
115-
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
116-
117124
Write-PSFMessage -Message "Started removing database clones" -Level Verbose
118125

119126
# Get all the items
@@ -160,6 +167,21 @@
160167
Stop-PSFFunction -Message "Could not connect to Sql Server instance $($clone.Name)" -ErrorRecord $_ -Target $clone.Name -Continue
161168
}
162169

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+
163185
# Loop through each of the results
164186
foreach ($item in $clone.Group) {
165187
$item
@@ -177,7 +199,14 @@
177199
try {
178200
if (Test-Path -Path $item.CloneLocation) {
179201
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+
}
181210
}
182211
}
183212
catch {
@@ -186,14 +215,29 @@
186215

187216
# Remove clone file and related access path
188217
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+
}
192228
}
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+
}
197241
}
198242
}
199243
catch {
@@ -204,7 +248,7 @@
204248
try {
205249
$query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);"
206250

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
208252
}
209253
catch {
210254
Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue

0 commit comments

Comments
 (0)