How to Fix PowerShell DSC Network Share Copy Error: “SourcePath Must Be a Directory”


3 views

When attempting to use PowerShell Desired State Configuration (DSC) to copy folder contents from a network share, many administrators encounter the frustrating error:

The path cannot point to the root directory or to the root of a net share.
SourcePath must be specified if you want to configure the destination directory recursively.

The DSC File resource has specific requirements for network paths. The key problems are:

  • DSC requires explicit subfolder paths, not just the share root
  • The UNC path format needs proper escaping
  • Credential delegation is often required for network access

Here's the corrected implementation that properly handles network shares:

Configuration TestSetup {
    Node localhost {
        File Test {
            SourcePath = "\\Server\SomeShare\SomeFolder\*"
            DestinationPath = "E:\test"
            Recurse = $true
            Type = "Directory"
            Credential = Get-Credential
        }
    }
}

Key modifications:

1. Added wildcard (*) to specify contents rather than root
2. Included Credential parameter for authentication
3. Used proper escaping for UNC paths

For more complex scenarios, consider these approaches:

# Using a specific subfolder
SourcePath = "\\Server\SomeShare\SomeFolder\Subfolder\*"

# Alternative with backtick escaping
SourcePath = "\\Server\SomeShare\SomeFolder\*"

# With pre-configured credentials
$cred = New-Object System.Management.Automation.PSCredential ("domain\user", (ConvertTo-SecureString "password" -AsPlainText -Force))
...
Credential = $cred
  • Double-check permissions on both source and destination
  • Verify network connectivity before running DSC
  • Test with a small folder first when using Recurse
  • Consider using xRemoteFile for large downloads

If the File resource still causes issues, consider these alternatives:

# Using Script resource
Script CopyFromShare {
    GetScript = { @{ Result = "CopyFromShare" } }
    SetScript = {
        Copy-Item -Path "\\Server\SomeShare\SomeFolder\*" -Destination "E:\test" -Recurse -Force
    }
    TestScript = { Test-Path "E:\test\somefile.txt" }
}

# Using xRemoteFile from xPSDesiredStateConfiguration
File DirectoryCopy {
    SourcePath = "\\Server\SomeShare\SomeFolder"
    DestinationPath = "E:\test"
    MatchSource = $true
    Recurse = $true
    Credential = $cred
}

When working with PowerShell Desired State Configuration (DSC), copying files from network shares presents unique authentication and path handling challenges. The common error occurs when the File resource attempts to access UNC paths without proper credentials or when the path format isn't properly specified.

The error message indicates two critical issues:

  • DSC cannot use root-level network shares (\\Server\Share)
  • The configuration lacks proper credential delegation for network access

Here's an improved configuration that handles network share access properly:

Configuration TestSetup {
    Node localhost {
        File Test {
            SourcePath = "\\\\Server\\SomeShare\\SomeFolder\\*"
            DestinationPath = "E:\\test"
            Recurse = $true
            Type = "Directory"
            Credential = Get-Credential
            MatchSource = $true
            Force = $true
        }
    }
}
  • Added wildcard (*) to indicate we're copying contents, not the share root
  • Included Credential parameter for proper authentication
  • Added MatchSource to ensure file verification
  • Used Force to handle any existing files

For more complex scenarios, consider using the Script resource:

Configuration AdvancedCopy {
    Node localhost {
        Script NetworkCopy {
            GetScript = { @{ Result = (Test-Path "E:\\test") } }
            SetScript = {
                $source = "\\\\Server\\SomeShare\\SomeFolder"
                $dest = "E:\\test"
                New-Item -ItemType Directory -Path $dest -Force
                Copy-Item -Path "$source\\*" -Destination $dest -Recurse -Force
            }
            TestScript = {
                $sourceCount = (Get-ChildItem "\\\\Server\\SomeShare\\SomeFolder" | Measure-Object).Count
                $destCount = (Get-ChildItem "E:\\test" | Measure-Object).Count
                return ($sourceCount -eq $destCount)
            }
            PsDscRunAsCredential = Get-Credential
        }
    }
}
  • Always test share accessibility before DSC runs (Test-Path, Get-SmbConnection)
  • Consider persistent mapped drives for frequently accessed shares
  • Implement proper error handling in your TestScript blocks
  • Document credential requirements in your configuration metadata