Skip to content

Commit ccc2422

Browse files
committed
Added support for SupportShouldProcess
1 parent 0836515 commit ccc2422

6 files changed

Lines changed: 444 additions & 379 deletions

File tree

functions/New-PSDCClone.ps1

Lines changed: 139 additions & 122 deletions
Large diffs are not rendered by default.

functions/New-PSDCImage.ps1

Lines changed: 153 additions & 128 deletions
Large diffs are not rendered by default.

functions/Remove-PSDCClone.ps1

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -184,68 +184,76 @@
184184

185185
# Loop through each of the results
186186
foreach ($item in $clone.Group) {
187-
$item
188-
# Remove the database
189-
try {
190-
Write-PSFMessage -Message "Removing database $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose
191-
192-
$null = Remove-DbaDatabase -SqlInstance $item.SqlInstance -SqlCredential $SqlCredential -Database $item.DatabaseName -Confirm:$false -EnableException
193-
}
194-
catch {
195-
Stop-PSFFunction -Message "Could not remove database $($item.DatabaseName) from $server" -ErrorRecord $_ -Target $server -Continue
196-
}
197187

198-
# Dismounting the vhd
199-
try {
188+
if ($PSCmdlet.ShouldProcess($item.DatabaseName, "Removing database $($item.DatabaseName)")) {
189+
# Remove the database
190+
try {
191+
Write-PSFMessage -Message "Removing database $($item.DatabaseName) from $($item.SqlInstance)" -Level Verbose
200192

201-
if ($computer.IsLocalhost) {
202-
$null = Dismount-VHD -Path $item.CloneLocation -Credential $Credential
193+
$null = Remove-DbaDatabase -SqlInstance $item.SqlInstance -SqlCredential $SqlCredential -Database $item.DatabaseName -Confirm:$false -EnableException
203194
}
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-
#}
195+
catch {
196+
Stop-PSFFunction -Message "Could not remove database $($item.DatabaseName) from $server" -ErrorRecord $_ -Target $server -Continue
212197
}
213198
}
214-
catch {
215-
Stop-PSFFunction -Message "Could not dismount vhd $($item.CloneLocation)" -ErrorRecord $_ -Target $result -Continue
216-
}
217-
218-
# Remove clone file and related access path
219-
try {
220-
if ($computer.IsLocalhost) {
221-
Write-PSFMessage -Message "Removing vhd access path" -Level Verbose
222-
$null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force
223199

224-
Write-PSFMessage -Message "Removing vhd" -Level Verbose
225-
$null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force
200+
if ($PSCmdlet.ShouldProcess($item.CloneLocation, "Dismounting the vhd")) {
201+
# Dismounting the vhd
202+
try {
203+
204+
if ($computer.IsLocalhost) {
205+
$null = Dismount-VHD -Path $item.CloneLocation -Credential $Credential
206+
}
207+
else {
208+
$command = [ScriptBlock]::Create("Test-Path -Path $($item.CloneLocation)")
209+
Write-PSFMessage -Message "Dismounting disk $($item.CloneLocation) from $($item.HostName)" -Level Verbose
210+
$result = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
211+
#if (-not $result) {
212+
$command = [scriptblock]::Create("Dismount-VHD -Path `"$($item.CloneLocation)`"")
213+
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
214+
#}
215+
}
226216
}
227-
else {
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
217+
catch {
218+
Stop-PSFFunction -Message "Could not dismount vhd $($item.CloneLocation)" -ErrorRecord $_ -Target $result -Continue
235219
}
236220
}
237-
catch {
238-
Stop-PSFFunction -Message "Could not remove clone files" -ErrorRecord $_ -Target $result -Continue
221+
222+
if ($PSCmdlet.ShouldProcess($item.CloneLocation, "Removing clone files and folders")) {
223+
# Remove clone file and related access path
224+
try {
225+
if ($computer.IsLocalhost) {
226+
Write-PSFMessage -Message "Removing vhd access path" -Level Verbose
227+
$null = Remove-Item -Path $item.AccessPath -Credential $Credential -Force
228+
229+
Write-PSFMessage -Message "Removing vhd" -Level Verbose
230+
$null = Remove-Item -Path $item.CloneLocation -Credential $Credential -Force
231+
}
232+
else {
233+
Write-PSFMessage -Message "Removing vhd access path" -Level Verbose
234+
$command = [scriptblock]::Create("Remove-Item -Path $($item.AccessPath) -Force")
235+
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
236+
237+
Write-PSFMessage -Message "Removing vhd" -Level Verbose
238+
$command = [scriptblock]::Create("Remove-Item -Path $($item.CloneLocation) -Force")
239+
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
240+
}
241+
}
242+
catch {
243+
Stop-PSFFunction -Message "Could not remove clone files" -ErrorRecord $_ -Target $result -Continue
244+
}
239245
}
240246

241-
# Removing records from database
242-
try {
243-
$query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);"
247+
if ($PSCmdlet.ShouldProcess($item.CloneID, "Deleting clone rom database")) {
248+
# Removing records from database
249+
try {
250+
$query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);"
244251

245-
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query -EnableException
246-
}
247-
catch {
248-
Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue
252+
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query -EnableException
253+
}
254+
catch {
255+
Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue
256+
}
249257
}
250258

251259
} # End for each group item

functions/Remove-PSDCImage.ps1

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,15 @@
196196

197197
# Loop through the results
198198
foreach ($result in $results) {
199-
200-
# Remove the clones for the host
201-
try {
202-
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
203-
Remove-PSDCClone -HostName $result.HostName -Database $result.DatabaseName -PSDCSqlCredential $pdcCredential -Credential $Credential -Confirm:$false
204-
}
205-
catch {
206-
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
199+
if ($PSCmdlet.ShouldProcess($item.CloneID, "Removing clone $($result.CloneLocation) from $($result.HostName)")) {
200+
# Remove the clones for the host
201+
try {
202+
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
203+
Remove-PSDCClone -HostName $result.HostName -Database $result.DatabaseName -PSDCSqlCredential $pdcCredential -Credential $Credential -Confirm:$false
204+
}
205+
catch {
206+
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
207+
}
207208
}
208209
}
209210
}
@@ -215,43 +216,47 @@
215216
Stop-PSFFunction -Message "Couldn't retrieve clone records for host $($result.HostName)" -ErrorRecord $_ -Target $hst -Continue
216217
}
217218

218-
# Remove the image from the file system
219-
try {
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
219+
if ($PSCmdlet.ShouldProcess($item.ImageLocation, "Removing image '$($item.ImageLocation)' from system")) {
220+
# Remove the image from the file system
221+
try {
222+
if ($computer.IsLocalhost) {
223+
if (Test-Path -Path $item.ImageLocation -Credential $Credential) {
224+
Write-PSFMessage -Message "Removing image '$($item.ImageLocation)' from file system" -Level Verbose
223225

224-
$null = Remove-Item -Path $item.ImageLocation -Credential $Credential -Force:$Force
226+
$null = Remove-Item -Path $item.ImageLocation -Credential $Credential -Force:$Force
227+
}
228+
else {
229+
Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose
230+
}
225231
}
226232
else {
227-
Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose
228-
}
229-
}
230-
else {
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")
233+
$command = [scriptblock]::Create("Test-Path -Path $($item.ImageLocation)")
236234
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
237-
}
238-
else {
239-
Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose
235+
236+
if ($result) {
237+
$command = [scriptblock]::Create("Remove-Item -Path $($item.ImageLocation) -Force")
238+
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
239+
}
240+
else {
241+
Write-PSFMessage -Message "Couldn't find image $($item.ImageLocation)" -Level Verbose
242+
}
240243
}
241244
}
242-
}
243-
catch {
244-
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from file system" -ErrorRecord $_ -Target $result
245+
catch {
246+
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from file system" -ErrorRecord $_ -Target $result
247+
}
245248
}
246249

247-
# Remove the image from the database
248-
try {
249-
$query = "DELETE FROM dbo.Image WHERE ImageID = $($item.ImageID)"
250+
if ($PSCmdlet.ShouldProcess($item.ImageLocation, "Removing image from system database")) {
251+
# Remove the image from the database
252+
try {
253+
$query = "DELETE FROM dbo.Image WHERE ImageID = $($item.ImageID)"
250254

251-
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query
252-
}
253-
catch {
254-
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from database" -ErrorRecord $_ -Target $query
255+
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query
256+
}
257+
catch {
258+
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from database" -ErrorRecord $_ -Target $query
259+
}
255260
}
256261

257262
} # End for each item in group

internal/functions/Initialize-PSDCVhdDisk.ps1

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function Initialize-PSDCVhdDisk {
2-
<#
2+
<#
33
.SYNOPSIS
44
Initialize-PSDCVhdDisk initialized the VHD
55
@@ -55,7 +55,7 @@
5555
5656
#>
5757

58-
[CmdLetBinding()]
58+
[CmdLetBinding(SupportsShouldProcess = $true)]
5959

6060
Param(
6161
[Parameter(Mandatory = $true)]
@@ -90,44 +90,50 @@
9090
$disk = $disks | Where-Object {$_.Location -eq $Path}
9191
}
9292
else {
93-
# Mount the vhd
94-
try {
95-
Write-PSFMessage -Message "Mounting disk $disk" -Level Verbose
96-
97-
$disk = Mount-VHD -Path $Path -PassThru | Get-Disk
93+
if ($PSCmdlet.ShouldProcess($disk, "Mounting disk")) {
94+
# Mount the vhd
95+
try {
96+
Write-PSFMessage -Message "Mounting disk $disk" -Level Verbose
97+
98+
$disk = Mount-VHD -Path $Path -PassThru | Get-Disk
99+
}
100+
catch {
101+
Stop-PSFFunction -Message "Couldn't mount vhd" -Target $Path -ErrorRecord $_ -Continue
102+
}
98103
}
99-
catch {
100-
Stop-PSFFunction -Message "Couldn't mount vhd" -Target $Path -ErrorRecord $_ -Continue
104+
}
105+
106+
if ($PSCmdlet.ShouldProcess($disk, "Initializing disk")) {
107+
# Check if the disk is already initialized
108+
if ($disk.PartitionStyle -eq 'RAW') {
109+
try {
110+
Write-PSFMessage -Message "Initializing disk $disk" -Level Verbose
111+
$disk | Initialize-Disk -PartitionStyle $PartitionStyle -Confirm:$false
112+
}
113+
catch {
114+
Stop-PSFFunction -Message "Couldn't initialize disk" -Target $disk -ErrorRecord $_ -Continue
115+
}
101116
}
102117
}
103118

104-
# Check if the disk is already initialized
105-
if ($disk.PartitionStyle -eq 'RAW') {
119+
if ($PSCmdlet.ShouldProcess($disk, "Partitioning volume")) {
120+
# Create the partition, set the drive letter and format the volume
106121
try {
107-
Write-PSFMessage -Message "Initializing disk $disk" -Level Verbose
108-
$disk | Initialize-Disk -PartitionStyle $PartitionStyle -Confirm:$false
122+
$volume = Get-Disk -Number $disk.Number | New-Partition -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "PSDatabaseClone" -AllocationUnitSize $AllocationUnitSize -Confirm:$false
109123
}
110124
catch {
111-
Stop-PSFFunction -Message "Couldn't initialize disk" -Target $disk -ErrorRecord $_ -Continue
112-
}
113-
}
125+
# Dismount the drive
126+
Dismount-VHD -Path $Path
114127

115-
# Create the partition, set the drive letter and format the volume
116-
try {
117-
$volume = Get-Disk -Number $disk.Number | New-Partition -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "PSDatabaseClone" -AllocationUnitSize $AllocationUnitSize -Confirm:$false
118-
}
119-
catch {
120-
# Dismount the drive
121-
Dismount-VHD -Path $Path
122-
123-
Stop-PSFFunction -Message "Couldn't create the partition" -Target $disk -ErrorRecord $_ -Continue
128+
Stop-PSFFunction -Message "Couldn't create the partition" -Target $disk -ErrorRecord $_ -Continue
129+
}
124130
}
125131

126132
# Add the results to the custom object
127133
[PSCustomObject]@{
128-
Disk = $disk
129-
Partition = (Get-Partition -Disk $disk)
130-
Volume = $volume
134+
Disk = $disk
135+
Partition = (Get-Partition -Disk $disk)
136+
Volume = $volume
131137
}
132138

133139
}

internal/functions/New-PSDCVhdDisk.ps1

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@
7373
begin {
7474
# Check the destination path
7575
if (-not (Test-Path $Destination)) {
76-
try {
77-
Write-PSFMessage -Message "Creating destination directory $Destination" -Level Verbose
78-
$null = New-Item -Path $Destination -ItemType Directory -Force
79-
}
80-
catch {
81-
Stop-PSFFunction -Message "Couldn't create directory $Destination" -ErrorRecord $_ -Target $Destination -Continue
76+
if ($PSCmdlet.ShouldProcess($Destination, "Creating destination directory")) {
77+
try {
78+
Write-PSFMessage -Message "Creating destination directory $Destination" -Level Verbose
79+
$null = New-Item -Path $Destination -ItemType Directory -Force
80+
}
81+
catch {
82+
Stop-PSFFunction -Message "Couldn't create directory $Destination" -ErrorRecord $_ -Target $Destination -Continue
83+
}
8284
}
8385
}
8486

@@ -123,18 +125,20 @@
123125
# Test if there are any errors
124126
if (Test-PSFFunctionInterrupt) { return }
125127

126-
# Check if the file needs to have a fixed size
127-
try {
128-
if ($FixedSize) {
129-
$null = New-VHD -Path $vhdPath -SizeBytes $Size -Fixed
128+
if ($PSCmdlet.ShouldProcess($vhdPath, "Creating VHD")) {
129+
# Check if the file needs to have a fixed size
130+
try {
131+
if ($FixedSize) {
132+
$null = New-VHD -Path $vhdPath -SizeBytes $Size -Fixed
133+
}
134+
else {
135+
$null = New-VHD -Path $vhdPath -SizeBytes $Size -Dynamic
136+
}
130137
}
131-
else {
132-
$null = New-VHD -Path $vhdPath -SizeBytes $Size -Dynamic
138+
catch {
139+
Stop-PSFFunction -Message "Something went wrong creating the vhd" -ErrorRecord $_ -Continue
133140
}
134141
}
135-
catch {
136-
Stop-PSFFunction -Message "Something went wrong creating the vhd" -ErrorRecord $_ -Continue
137-
}
138142
}
139143

140144
end {

0 commit comments

Comments
 (0)