Skip to content

Commit 4779263

Browse files
committed
Fixed remote execution
1 parent bd47e7b commit 4779263

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

functions/Remove-PSDCClone.ps1

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,18 @@
197197

198198
# Dismounting the vhd
199199
try {
200-
if (Test-Path -Path $item.CloneLocation) {
201-
Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose
202200

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-
}
201+
if ($computer.IsLocalhost) {
202+
$null = Dismount-VHD -Path $item.CloneLocation -Credential $Credential
203+
}
204+
else {
205+
$command = [ScriptBlock]::Create("Test-Path -Path $($item.CloneLocation)")
206+
Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose
207+
$result = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
208+
#if (-not $result) {
209+
$command = [scriptblock]::Create("Dismount-VHD -Path `"$($item.CloneLocation)`"")
210+
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
211+
#}
210212
}
211213
}
212214
catch {
@@ -216,28 +218,20 @@
216218
# Remove clone file and related access path
217219
try {
218220
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-
}
221+
Write-PSFMessage -Message "Removing vhd access path" -Level Verbose
222+
$null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force
223+
224+
Write-PSFMessage -Message "Removing vhd" -Level Verbose
225+
$null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force
228226
}
229227
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-
}
228+
Write-PSFMessage -Message "Removing vhd access path" -Level Verbose
229+
$command = [scriptblock]::Create("Remove-Item -Path $($item.AccessPath) -Force")
230+
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
231+
232+
Write-PSFMessage -Message "Removing vhd" -Level Verbose
233+
$command = [scriptblock]::Create("Remove-Item -Path $($item.CloneLocation) -Force")
234+
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
241235
}
242236
}
243237
catch {

0 commit comments

Comments
 (0)