|
196 | 196 |
|
197 | 197 | # Check destination
|
198 | 198 | if (-not $Destination) {
|
199 |
| - $Destination = "$($server.DefaultFile)\clone" |
| 199 | + $Destination = $server.DefaultFile |
| 200 | + if ($server.DefaultFile.EndsWith("\")) { |
| 201 | + $Destination = $Destination.Substring(0, $Destination.Length - 1) |
| 202 | + } |
| 203 | + |
| 204 | + $Destination += "\clone" |
200 | 205 | }
|
201 | 206 | else {
|
202 | 207 | # If the destination is a network path
|
|
229 | 234 | # Test if the destination can be reached
|
230 | 235 | # Check if computer is local
|
231 | 236 | if ($computer.IsLocalhost) {
|
232 |
| - if (-not (Test-Path -Path $Destination -Credential $Credential)) { |
| 237 | + if (-not (Test-Path -Path $Destination)) { |
233 | 238 | Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
|
234 | 239 | }
|
235 | 240 | }
|
236 | 241 | else {
|
237 |
| - $command = [ScriptBlock]::Create("Test-Path -Path $Destination") |
| 242 | + $command = [ScriptBlock]::Create("Test-Path -Path '$Destination'") |
238 | 243 | $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
239 | 244 | if (-not $result) {
|
240 | 245 | Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
|
|
271 | 276 | }
|
272 | 277 | }
|
273 | 278 | else {
|
274 |
| - $command = [scriptblock]::Create("Test-Path -Path $ParentVhd") |
| 279 | + $command = [scriptblock]::Create("Test-Path -Path '$ParentVhd'") |
275 | 280 | $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
276 | 281 | if ($result) {
|
277 | 282 | $parentVhdFileName = $ParentVhd.Split("\")[-1]
|
|
319 | 324 | }
|
320 | 325 | }
|
321 | 326 | else {
|
322 |
| - $command = [ScriptBlock]::Create("Test-Path -Path $accessPath") |
| 327 | + $command = [ScriptBlock]::Create("Test-Path -Path '$accessPath'") |
323 | 328 | $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
324 | 329 | if (-not $result) {
|
325 | 330 | try {
|
326 |
| - $command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Force") |
| 331 | + $command = [ScriptBlock]::Create("New-Item -Path '$accessPath' -ItemType Directory -Force") |
327 | 332 | $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
328 | 333 | }
|
329 | 334 | catch {
|
|
427 | 432 | $command = [ScriptBlock]::Create("Get-Partition -DiskNumber $($disk.Number)")
|
428 | 433 | $partition = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
429 | 434 |
|
430 |
| - $command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $($disk.Number) -PartitionNumber $($partition[1].PartitionNumber) -AccessPath $accessPath -ErrorAction Ignore") |
| 435 | + $command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $($disk.Number) -PartitionNumber $($partition[1].PartitionNumber) -AccessPath '$accessPath' -ErrorAction Ignore") |
431 | 436 |
|
432 | 437 | $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
433 | 438 | }
|
|
438 | 443 | }
|
439 | 444 | }
|
440 | 445 |
|
| 446 | + # Set privileges for access path |
| 447 | + try { |
| 448 | + # Check if computer is local |
| 449 | + if ($computer.IsLocalhost) { |
| 450 | + $accessRule = New-Object System.Security.AccessControl.FilesystemAccessrule("Everyone", "FullControl", "Allow") |
| 451 | + |
| 452 | + foreach ($file in $(Get-ChildItem $accessPath -Recurse)) { |
| 453 | + $acl = Get-Acl $file.FullName |
| 454 | + |
| 455 | + # Add this access rule to the ACL |
| 456 | + $acl.SetAccessRule($accessRule) |
| 457 | + |
| 458 | + # Write the changes to the object |
| 459 | + Set-Acl -Path $file.Fullname -AclObject $acl |
| 460 | + } |
| 461 | + } |
| 462 | + else { |
| 463 | + [string]$commandText = "`$accessRule = New-Object System.Security.AccessControl.FilesystemAccessrule(`"Everyone`", `"FullControl`", `"Allow`") |
| 464 | + foreach (`$file in `$(Get-ChildItem -Path `"$accessPath`" -Recurse)) { |
| 465 | + `$acl = Get-Acl `$file.Fullname |
| 466 | +
|
| 467 | + # Add this access rule to the ACL |
| 468 | + `$acl.SetAccessRule(`$accessRule) |
| 469 | +
|
| 470 | + # Write the changes to the object |
| 471 | + Set-Acl -Path `$file.Fullname -AclObject `$acl |
| 472 | + }" |
| 473 | + |
| 474 | + $command = [scriptblock]::Create($commandText) |
| 475 | + |
| 476 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -ArgumentList $accessPath -Credential $DestinationCredential |
| 477 | + } |
| 478 | + } |
| 479 | + catch { |
| 480 | + Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue |
| 481 | + } |
| 482 | + |
441 | 483 | # Get all the files of the database
|
442 | 484 | if ($computer.IsLocalhost) {
|
443 | 485 | $databaseFiles = Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $_.PSIsContainer}
|
444 | 486 | }
|
445 | 487 | else {
|
446 |
| - $commandText = "Get-ChildItem -Path $accessPath -Recurse | " + 'Where-Object {-not $_.PSIsContainer}' |
| 488 | + $commandText = "Get-ChildItem -Path '$accessPath' -Recurse | " + 'Where-Object {-not $_.PSIsContainer}' |
447 | 489 | $command = [ScriptBlock]::Create($commandText)
|
448 | 490 | $databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
|
449 | 491 | }
|
|
0 commit comments