|
1 | 1 | function New-PSDCImage { |
2 | | -<# |
| 2 | + <# |
3 | 3 | .SYNOPSIS |
4 | 4 | New-PSDCImage creates a new image |
5 | 5 |
|
|
165 | 165 | # Get the local path from the network path |
166 | 166 | if (-not $ImageLocalPath) { |
167 | 167 | try { |
| 168 | + # Check if computer is local |
168 | 169 | if ($computer.IsLocalhost) { |
169 | 170 | $ImageLocalPath = Convert-PSDCLocalUncPathToLocalPath -UncPath $ImageNetworkPath |
170 | 171 | } |
|
265 | 266 | # try to create the new VHD |
266 | 267 | try { |
267 | 268 | Write-PSFMessage -Message "Create the vhd $imageName.vhdx" -Level Verbose |
268 | | - $null = New-PSDCVhdDisk -Destination $imagePath -FileName "$imageName.vhdx" |
| 269 | + |
| 270 | + # Check if computer is local |
| 271 | + if ($computer.IsLocalhost) { |
| 272 | + $null = New-PSDCVhdDisk -Destination $imagePath -FileName "$imageName.vhdx" |
| 273 | + } |
| 274 | + else { |
| 275 | + $command = [ScriptBlock]::Create("$null = New-PSDCVhdDisk -Destination $imagePath -FileName '$imageName.vhdx'") |
| 276 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 277 | + } |
| 278 | + |
269 | 279 | } |
270 | 280 | catch { |
271 | 281 | Stop-PSFFunction -Message "Couldn't create vhd $imageName" -Target "$imageName.vhd" -ErrorRecord $_ -Continue |
|
275 | 285 | try { |
276 | 286 | Write-PSFMessage -Message "Initializing the vhd $imageName.vhd" -Level Verbose |
277 | 287 |
|
278 | | - $diskResult = Initialize-PSDCVhdDisk -Path $vhdPath -Credential $DestinationCredential |
| 288 | + # Check if computer is local |
| 289 | + if ($computer.IsLocalhost) { |
| 290 | + $diskResult = Initialize-PSDCVhdDisk -Path $vhdPath -Credential $DestinationCredential |
| 291 | + } |
| 292 | + else { |
| 293 | + $command = [ScriptBlock]::Create("Initialize-PSDCVhdDisk -Path $vhdPath -Credential $DestinationCredential") |
| 294 | + $diskResult = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 295 | + } |
279 | 296 | } |
280 | 297 | catch { |
281 | 298 | Stop-PSFFunction -Message "Couldn't initialize vhd $vhdPath" -Target $imageName -ErrorRecord $_ -Continue |
|
287 | 304 | # Check if access path is already present |
288 | 305 | if (-not (Test-Path -Path $accessPath)) { |
289 | 306 | try { |
290 | | - $null = New-Item -Path $accessPath -ItemType Directory -Force:$Force |
| 307 | + # Check if computer is local |
| 308 | + if ($computer.IsLocalhost) { |
| 309 | + $null = New-Item -Path $accessPath -ItemType Directory -Force:$Force |
| 310 | + } |
| 311 | + else { |
| 312 | + $command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Force:$Force") |
| 313 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 314 | + } |
291 | 315 | } |
292 | 316 | catch { |
293 | 317 | Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue |
|
299 | 323 | $partition = $diskResult.Partition |
300 | 324 |
|
301 | 325 | # Add the access path to the mounted disk |
302 | | - $null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction SilentlyContinue |
| 326 | + if ($computer.IsLocalhost) { |
| 327 | + $null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction SilentlyContinue |
| 328 | + } |
| 329 | + else { |
| 330 | + $command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction SilentlyContinue") |
| 331 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 332 | + } |
| 333 | + |
303 | 334 | } |
304 | 335 | catch { |
305 | 336 | Stop-PSFFunction -Message "Couldn't create access path for partition" -ErrorRecord $_ -Target $diskResult.partition |
|
309 | 340 | $imageDataFolder = "$($imagePath)\$imageName\Data" |
310 | 341 | $imageLogFolder = "$($imagePath)\$imageName\Log" |
311 | 342 |
|
312 | | - # Check if image folder structure exist |
| 343 | + # Check if image data folder exist |
313 | 344 | if (-not (Test-Path -Path $imageDataFolder)) { |
314 | 345 | try { |
315 | 346 | Write-PSFMessage -Message "Creating data folder for image" -Level Verbose |
316 | | - $null = New-Item -Path $imageDataFolder -ItemType Directory |
| 347 | + |
| 348 | + # Check if computer is local |
| 349 | + if ($computer.IsLocalhost) { |
| 350 | + $null = New-Item -Path $imageDataFolder -ItemType Directory |
| 351 | + } |
| 352 | + else { |
| 353 | + $command = [ScriptBlock]::Create("New-Item -Path $imageDataFolder -ItemType Directory") |
| 354 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 355 | + } |
317 | 356 | } |
318 | 357 | catch { |
319 | 358 | Stop-PSFFunction -Message "Couldn't create image data folder" -Target $imageName -ErrorRecord $_ -Continue |
320 | 359 | } |
321 | 360 | } |
322 | 361 |
|
| 362 | + # Test if the image log folder exists |
323 | 363 | if (-not (Test-Path -Path $imageLogFolder)) { |
324 | 364 | try { |
325 | 365 | Write-PSFMessage -Message "Creating transaction log folder for image" -Level Verbose |
326 | | - $null = New-Item -Path $imageLogFolder -ItemType Directory |
| 366 | + |
| 367 | + # Check if computer is local |
| 368 | + if ($computer.IsLocalhost) { |
| 369 | + $null = New-Item -Path $imageLogFolder -ItemType Directory |
| 370 | + } |
| 371 | + else { |
| 372 | + $command = [ScriptBlock]::Create("New-Item -Path $imageLogFolder -ItemType Directory") |
| 373 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 374 | + } |
| 375 | + |
327 | 376 | } |
328 | 377 | catch { |
329 | 378 | Stop-PSFFunction -Message "Couldn't create image data folder" -Target $imageName -ErrorRecord $_ -Continue |
|
336 | 385 | # Restore database to image folder |
337 | 386 | try { |
338 | 387 | Write-PSFMessage -Message "Restoring database $db on $DestinationSqlInstance" -Level Verbose |
339 | | - |
340 | 388 | $restore = Restore-DbaDatabase -SqlInstance $DestinationSqlInstance -SqlCredential $DestinationSqlCredential ` |
341 | 389 | -DatabaseName $tempDbName -Path $lastFullBackup ` |
342 | 390 | -DestinationDataDirectory $imageDataFolder ` |
|
349 | 397 | # Detach database |
350 | 398 | try { |
351 | 399 | Write-PSFMessage -Message "Detaching database $tempDbName on $DestinationSqlInstance" -Level Verbose |
352 | | - |
353 | 400 | $null = Dismount-DbaDatabase -SqlInstance $DestinationSqlInstance -Database $tempDbName -SqlCredential $DestinationSqlCredential |
354 | 401 | } |
355 | 402 | catch { |
|
360 | 407 | try { |
361 | 408 | Write-PSFMessage -Message "Dismounting vhd" -Level Verbose |
362 | 409 |
|
363 | | - # Dismount the VHD |
364 | | - $null = Dismount-VHD -Path $vhdPath |
| 410 | + # Check if computer is local |
| 411 | + if ($computer.IsLocalhost) { |
| 412 | + # Dismount the VHD |
| 413 | + $null = Dismount-VHD -Path $vhdPath |
365 | 414 |
|
366 | | - # Remove the access path |
367 | | - $null = Remove-Item -Path $accessPath -Force |
| 415 | + # Remove the access path |
| 416 | + $null = Remove-Item -Path $accessPath -Force |
| 417 | + } |
| 418 | + else { |
| 419 | + $command = [ScriptBlock]::Create("Dismount-VHD -Path $vhdPath") |
| 420 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 421 | + |
| 422 | + $command = [ScriptBlock]::Create("Remove-Item -Path $accessPath -Force") |
| 423 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential |
| 424 | + } |
368 | 425 | } |
369 | 426 | catch { |
370 | 427 | Stop-PSFFunction -Message "Couldn't dismount vhd" -Target $imageName -ErrorRecord $_ -Continue |
|
377 | 434 | $databaseTS = $lastFullBackup.Start |
378 | 435 |
|
379 | 436 | $query = " |
380 | | - DECLARE @ImageID INT; |
381 | | - EXECUTE dbo.Image_New @ImageID = @ImageID OUTPUT, -- int |
382 | | - @ImageName = '$imageName', -- varchar(100) |
383 | | - @ImageLocation = '$imageLocation', -- varchar(255) |
384 | | - @SizeMB = $sizeMB, -- int |
385 | | - @DatabaseName = '$databaseName', -- varchar(100) |
386 | | - @DatabaseTimestamp = '$databaseTS' -- datetime |
387 | | -
|
388 | | - SELECT @ImageID as ImageID |
389 | | - " |
| 437 | + DECLARE @ImageID INT; |
| 438 | + EXECUTE dbo.Image_New @ImageID = @ImageID OUTPUT, -- int |
| 439 | + @ImageName = '$imageName', -- varchar(100) |
| 440 | + @ImageLocation = '$imageLocation', -- varchar(255) |
| 441 | + @SizeMB = $sizeMB, -- int |
| 442 | + @DatabaseName = '$databaseName', -- varchar(100) |
| 443 | + @DatabaseTimestamp = '$databaseTS' -- datetime |
| 444 | +
|
| 445 | + SELECT @ImageID as ImageID |
| 446 | + " |
390 | 447 |
|
391 | 448 | Write-PSFMessage -Message "Query New Image`n$query" -Level Debug |
392 | 449 |
|
|
0 commit comments