1
votes

I've been trying to make a DSC Script work using DSC File Resource to copy files from Azure File Storage to local as shown below

 File FabrikamFibreSourceFiles
    {
        Ensure = "Present"  # You can also set Ensure to "Absent"
        Type = "Directory“ # Default is “File”
        Recurse = $true
        Credential = $storageCredential
        SourcePath = "\\sriksstore.file.core.windows.net\fabrikamfibreshare" # This is a path that has web files
        DestinationPath = "C:\inetpub\dev\fabrikamfibre\" # The path where we want to ensure the web files are present
    }

The $storageCredential is passed while invoking as shown below.

$storageContext = New-AzureStorageContext -StorageAccountName $storageAccountName  -StorageAccountKey $storageKey
$secpasswd = ConvertTo-SecureString $storageKey -AsPlainText -Force
$storagecreds = New-Object System.Management.Automation.PSCredential ($storageAccountName, $secpasswd)

Get-AzureVM -ServiceName fabrikamfibre -Name fabrikamfibre| `
Set-AzureVMDscExtension -StorageContext $storageContext `
-ConfigurationName "FabrikamFibre" -ConfigurationArgument @{ storageCredential= ($storagecreds) }`
-ConfigurationArchive "fabrikamfibredsc.ps1.zip" | Update-AzureVM

It fails to copy the contents with the following errors

An error occurs when accessing the network share with the specified credential. Please make sure the credential is correct and the network share is accessible. Note that Credential should not be specified with the local path. The related file/directory is: \sriksstore.file.core.windows.net\fabrikamfibreshare. A specified logon session does not exist. It may already have been terminated. An error occurs when accessing the network share with the specified credential. Please make sure the credential is correct and the network share is accessible. Note that Credential should not be specified with the local path. The related file/directory is: \sriksstore.file.core.windows.net\fabrikamfibreshare.

It also fails when I try to invoke using Get-Credential as mentioned in various samples, if anyone tried using DSC File resource with Azure File Storage, please help. I've double checked the credentials they are correct, interesting when I use write-verbose to print $storageCredential.Username they always output blank. I'm definitely missing something here.

2

2 Answers

0
votes

could you double-check the declaration of $storageCredential in your configuration and ensure it is of type PSCredential?

configuration MyConfiguration([PSCredential] $storageCredential...) {
   ...
} 

If that is correct, can your check the value of storageCredential that your VM received? It will be in a JSON object in a file named

c:\packages\plugins\Microsoft.powershell.dsc\x.x.x.x\runtimesettings\settings.x

0
votes

This is a pretty old question but unfortunately the problem still persists to this day when using the File resource for copying files from Azure fileshare. I attempted to use it in exactly the same manner the question author did via a DSC extension and got the same error. I verified all of the credentials were correct in the settings files. In fact, I had no problem connecting to the fileshare from the VM by using the file explorer.

I eventually gave up using File resource and used a Script resource instead where I manually mounted to fileshare with Powershell, copied the files to the local filesystem, and disconnected the mount. I am passing in some variables to the script such as the fileshare credential, the letter for the drive to mount to, and local file paths to drop the files. In the TestScript portion you can run whatever verification works for you, for example, use Test-Path to make sure the files were copied over. Hope this helps someone who stumbles into this issue...

    Script CopyFilesFromAzureFileshare
    {
        SetScript =
        {
             New-PSDrive -Name $using:localMountDrive -PSProvider FileSystem -Root $using:fileshareSrcPath -Credential $using:fileshareCredential -Persist

             $src = $using:localMountDrive + ":\*";

             if (-Not (Test-Path $using:localSrcPath))
             {
                  md -path $using:localSrcPath
             }

             Copy-Item $src -Destination $using:localSrcPath -Recurse -Force

             Remove-PSDrive -Name $using:localMountDrive -PSProvider FileSystem 
        }
        TestScript = 
        {
            Test-Path "$using:localSrcPath\$using:fileshareInstallArchive" -PathType Leaf

        }
        GetScript = { @{ Result = '' } }
    }