Skip to content

Commit c1a2388

Browse files
committed
Fixed remote execution. Fixes #19
1 parent 4779263 commit c1a2388

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

functions/Remove-PSDCImage.ps1

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
$pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null
9898
}
9999
else {
100-
$pdcCredential = $pdcCredential
100+
$pdcCredential = $PSDCSqlCredential
101101
}
102102

103103
# Test the module database setup
@@ -111,7 +111,7 @@
111111
Write-PSFMessage -Message "Started removing database images" -Level Verbose
112112

113113
# Get all the items
114-
$items = Get-PSDCImage -pdcCredential $pdcCredential
114+
$items = Get-PSDCImage -PSDCSqlCredential $pdcCredential
115115

116116
if ($ImageID) {
117117
Write-PSFMessage -Message "Filtering image ids" -Level Verbose
@@ -154,23 +154,36 @@
154154
# Loop through each of the results
155155
foreach ($item in $image.Group) {
156156

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+
157170
$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+
"
174187

175188
# Try to get the neccesary info from the EasyClone database
176189
try {
@@ -187,7 +200,7 @@
187200
# Remove the clones for the host
188201
try {
189202
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
191204
}
192205
catch {
193206
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
@@ -204,12 +217,27 @@
204217

205218
# Remove the image from the file system
206219
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+
}
210229
}
211230
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+
}
213241
}
214242
}
215243
catch {

0 commit comments

Comments
 (0)