Skip to content

Commit 6c7b083

Browse files
committed
Implemented extra checks. Fixes #7. Fixes #8
1 parent 7304971 commit 6c7b083

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

functions/image/New-PSDCImage.ps1

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@
112112
$DestinationCredential,
113113
[System.Management.Automation.PSCredential]
114114
$PSDCSqlCredential,
115-
[Parameter(Mandatory = $true)]
116-
[ValidateNotNullOrEmpty()]
117115
[string]$ImageNetworkPath,
118116
[string]$ImageLocalPath,
119117
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
@@ -131,6 +129,12 @@
131129
return
132130
}
133131

132+
# Checking parameters
133+
if (-not $ImageNetworkPath) {
134+
Stop-PSFFunction -Message "Please enter the network path where to save the images"
135+
return
136+
}
137+
134138
# Get the information store
135139
$informationStore = Get-PSFConfigValue -FullName psdatabaseclone.informationstore.mode
136140

@@ -207,12 +211,6 @@
207211
}
208212
}
209213

210-
# Check if Hyper-V is enabled
211-
if (-not (Test-PSDCHyperVEnabled -HostName $uriHost -Credential $DestinationCredential)) {
212-
Stop-PSFFunction -Message "Hyper-V is not enabled on the host." -ErrorRecord $_ -Target $uriHost
213-
return
214-
}
215-
216214
# Get the local path from the network path
217215
if (-not $ImageLocalPath) {
218216
if ($PSCmdlet.ShouldProcess($ImageNetworkPath, "Converting UNC path to local path")) {
@@ -232,13 +230,46 @@
232230
}
233231

234232
Write-PSFMessage -Message "Converted '$ImageNetworkPath' to '$ImageLocalPath'" -Level Verbose
233+
235234
}
236235
catch {
237236
Stop-PSFFunction -Message "Something went wrong getting the local image path" -Target $ImageNetworkPath
238237
return
239238
}
240239
}
241240
}
241+
else {
242+
# Cleanup the values in the network path
243+
if ($ImageLocalPath.EndsWith("\")) {
244+
$ImageLocalPath = $ImageLocalPath.Substring(0, $ImageLocalPath.Length - 1)
245+
}
246+
247+
# Check if the assigned value in the local path corresponds to the one retrieved
248+
try {
249+
# Check if computer is local
250+
if ($computer.IsLocalhost) {
251+
$convertedLocalPath = Convert-PSDCLocalUncPathToLocalPath -UncPath $ImageNetworkPath -EnableException
252+
}
253+
else {
254+
$command = "Convert-PSDCLocalUncPathToLocalPath -UncPath `"$ImageNetworkPath`" -EnableException"
255+
$commandGetLocalPath = [ScriptBlock]::Create($command)
256+
$convertedLocalPath = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $commandGetLocalPath -Credential $DestinationCredential
257+
}
258+
259+
Write-PSFMessage -Message "Converted '$ImageNetworkPath' to '$ImageLocalPath'" -Level Verbose
260+
261+
# Check if the ImageLocalPath and convertedLocalPath are the same
262+
if ($ImageLocalPath -ne $convertedLocalPath) {
263+
Stop-PSFFunction -Message "The local path '$ImageLocalPath' is not the same location as the network path '$ImageNetworkPath'" -Target $ImageNetworkPath
264+
return
265+
}
266+
267+
}
268+
catch {
269+
Stop-PSFFunction -Message "Something went wrong getting the local image path" -Target $ImageNetworkPath
270+
return
271+
}
272+
}
242273

243274
# Check the image local path
244275
if ($PSCmdlet.ShouldProcess("Verifying image local path")) {
@@ -269,6 +300,12 @@
269300
Stop-PSFFunction -Message "Please supply a database to create an image for" -Target $SourceSqlInstance -Continue
270301
}
271302

303+
# Check if Hyper-V is enabled
304+
if (-not (Test-PSDCHyperVEnabled -HostName $uriHost -Credential $DestinationCredential)) {
305+
Stop-PSFFunction -Message "Hyper-V is not enabled on the host." -ErrorRecord $_ -Target $uriHost
306+
return
307+
}
308+
272309
# Set time stamp
273310
$timestamp = Get-Date -format "yyyyMMddHHmmss"
274311

@@ -543,10 +580,10 @@
543580
[array]$images = $null
544581

545582
# Get all the images
546-
try{
583+
try {
547584
$images = Get-PSDCImage
548585
}
549-
catch{
586+
catch {
550587
Stop-PSFFunction -Message "Couldn't get images" -Target $imageName -ErrorRecord $_
551588
return
552589
}

0 commit comments

Comments
 (0)