Skip to content

Commit 76ce841

Browse files
committed
Fixed location of destination, folders with spaces, Setting Privileges
1 parent f1f43ab commit 76ce841

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

functions/clone/New-PSDCClone.ps1

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@
196196

197197
# Check destination
198198
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"
200205
}
201206
else {
202207
# If the destination is a network path
@@ -229,12 +234,12 @@
229234
# Test if the destination can be reached
230235
# Check if computer is local
231236
if ($computer.IsLocalhost) {
232-
if (-not (Test-Path -Path $Destination -Credential $Credential)) {
237+
if (-not (Test-Path -Path $Destination)) {
233238
Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
234239
}
235240
}
236241
else {
237-
$command = [ScriptBlock]::Create("Test-Path -Path $Destination")
242+
$command = [ScriptBlock]::Create("Test-Path -Path '$Destination'")
238243
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
239244
if (-not $result) {
240245
Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
@@ -271,7 +276,7 @@
271276
}
272277
}
273278
else {
274-
$command = [scriptblock]::Create("Test-Path -Path $ParentVhd")
279+
$command = [scriptblock]::Create("Test-Path -Path '$ParentVhd'")
275280
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
276281
if ($result) {
277282
$parentVhdFileName = $ParentVhd.Split("\")[-1]
@@ -319,11 +324,11 @@
319324
}
320325
}
321326
else {
322-
$command = [ScriptBlock]::Create("Test-Path -Path $accessPath")
327+
$command = [ScriptBlock]::Create("Test-Path -Path '$accessPath'")
323328
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
324329
if (-not $result) {
325330
try {
326-
$command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Force")
331+
$command = [ScriptBlock]::Create("New-Item -Path '$accessPath' -ItemType Directory -Force")
327332
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
328333
}
329334
catch {
@@ -427,7 +432,7 @@
427432
$command = [ScriptBlock]::Create("Get-Partition -DiskNumber $($disk.Number)")
428433
$partition = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
429434

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")
431436

432437
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
433438
}
@@ -438,12 +443,49 @@
438443
}
439444
}
440445

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+
441483
# Get all the files of the database
442484
if ($computer.IsLocalhost) {
443485
$databaseFiles = Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $_.PSIsContainer}
444486
}
445487
else {
446-
$commandText = "Get-ChildItem -Path $accessPath -Recurse | " + 'Where-Object {-not $_.PSIsContainer}'
488+
$commandText = "Get-ChildItem -Path '$accessPath' -Recurse | " + 'Where-Object {-not $_.PSIsContainer}'
447489
$command = [ScriptBlock]::Create($commandText)
448490
$databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
449491
}

0 commit comments

Comments
 (0)