Skip to content

Commit 0fa603b

Browse files
committed
Added import credential configuration. Added Hyper-V check for remote support. Moved variable declaration to correct spot
1 parent 90e6a46 commit 0fa603b

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

functions/New-PSDCImage.ps1

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@
129129
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server
130130
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
131131
if (-not $PSDCSqlCredential) {
132-
$pdcCredential = Get-PSFConfig -FullName psdatabaseclone.database.credential -Fallback $null
132+
$pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null
133133
}
134-
else{
134+
else {
135135
$pdcCredential = $PSDCSqlCredential
136136
}
137137

@@ -148,7 +148,7 @@
148148
# Try connecting to the instance
149149
Write-PSFMessage -Message "Attempting to connect to Sql Server $SourceSqlInstance.." -Level Output
150150
try {
151-
$SourceServer = Connect-DbaInstance -SqlInstance $SourceSqlInstance -SqlCredential $SourceSqlCredential
151+
$sourceServer = Connect-DbaInstance -SqlInstance $SourceSqlInstance -SqlCredential $SourceSqlCredential
152152
}
153153
catch {
154154
Stop-PSFFunction -Message "Could not connect to Sql Server instance $SourceSqlInstance" -ErrorRecord $_ -Target $SourceSqlInstance
@@ -172,8 +172,14 @@
172172
# Setup the computer object
173173
$computer = [PsfComputer]$uriHost
174174

175-
if (-not $computer.IsLocalhost) {
175+
# Check if Hyper-V is enabled
176+
if (-not (Test-PSDCHyperVEnabled -HostName $uriHost -Credential $DestinationCredential)) {
177+
Stop-PSFFunction -Message "Hyper-V is not enabled on the remote host." -ErrorRecord $_ -Target $uriHost
178+
return
179+
}
176180

181+
# Check if the computer is localhost and import the neccesary modules... just in case
182+
if (-not $computer.IsLocalhost) {
177183
$command = [ScriptBlock]::Create("Import-Module dbatools")
178184
Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
179185

@@ -182,9 +188,6 @@
182188

183189
$command = [ScriptBlock]::Create("Import-Module PSDatabaseClone")
184190
Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
185-
186-
$command = "Convert-PSDCLocalUncPathToLocalPath -UncPath '$ImageNetworkPath'"
187-
$commandGetLocalPath = [ScriptBlock]::Create($command)
188191
}
189192

190193
# Get the local path from the network path
@@ -195,8 +198,11 @@
195198
$ImageLocalPath = Convert-PSDCLocalUncPathToLocalPath -UncPath $ImageNetworkPath
196199
}
197200
else {
201+
$command = "Convert-PSDCLocalUncPathToLocalPath -UncPath `"$ImageNetworkPath`""
202+
$commandGetLocalPath = [ScriptBlock]::Create($command)
198203
$ImageLocalPath = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $commandGetLocalPath -Credential $DestinationCredential
199204
}
205+
200206
Write-PSFMessage -Message "Converted '$ImageNetworkPath' to '$ImageLocalPath'" -Level Verbose
201207
}
202208
catch {
@@ -224,12 +230,12 @@
224230
# Check the database parameter
225231
if ($Database) {
226232
foreach ($db in $Database) {
227-
if ($db -notin $SourceServer.Databases.Name) {
233+
if ($db -notin $sourceServer.Databases.Name) {
228234
Stop-PSFFunction -Message "Database $db cannot be found on instance $SourceSqlInstance" -Target $SourceSqlInstance
229235
}
230-
231-
$DatabaseCollection = $SourceServer.Databases | Where-Object { $_.Name -in $Database }
232236
}
237+
238+
$DatabaseCollection = $sourceServer.Databases | Where-Object { $_.Name -in $Database }
233239
}
234240
else {
235241
Stop-PSFFunction -Message "Please supply a database to create an image for" -Target $SourceSqlInstance -Continue

0 commit comments

Comments
 (0)