ChefDK and PowerShell DSC File and Script Resource


#1

Thanks in advance for any assistance.

I’m trying to refactor my code using the dsc_resource. I’m using the File resource in this instance. Having a few issues which hopefully someone can point me in the right direction.

I’m creating a folder as below, this works fine.
dsc_resource ‘Exchange2016Folder’ do
resource :File
property :Type, 'Directory’
property :DestinationPath, 'c:/Exchange2016’
property :Ensure, 'Present’
end

The issue I’m having is if I run the below without the credential line, I receive the error:
Chef::Exceptions::PowershellCmdletException: Powershell Cmdlet failed: Invoke-DscResource : Relative path is not supported. The related file/directory is: https://download.microsoft.com/download/3/9/B/39B8DDA8-509C-4B9E-BCE9-4CD8CDC9A7DA/Exchange2016-x64.exe.
I found a similar error here: http://serverfault.com/questions/568833/powershell-dsc-copy-from-network-share which indicates that the command might be run as a SYSTEM account.

dsc_resource ‘Exchange2016File’ do
resource :file
property :Credential, 'Chef::Util::Powershell:PSCredential(“vagrant”, “vagrant”)'
property :DestinationPath, 'c:/Exchange2016/Exchange2016-x64.exe’
property :SourcePath, 'https://download.microsoft.com/download/3/9/B/39B8DDA8-509C-4B9E-BCE9-4CD8CDC9A7DA/Exchange2016-x64.exe
COMMENTED OUT property :Checksum, '131520f0f703956643a1887b384f54d4d765beb882f201c2905a5c7e20410db5’
property :Checksum, "SHA-256"
property :Ensure, 'Present’
property :Type, 'File’
COMMENTED OUT property :MatchSource, $true
end

I’m trying to work out if I can download the file using a URI using the File DSC Resource and also how to us the Credential property correctly.

My alternative is to use the below dsc_resource Script resource to download the file.

dsc_resource ‘downloadExchange2016-x64fromMS’ do
resource :Script
property :GetScript, '{@{Result = (get-content c:/Exchange2016/Exchange2016-x64.exe).Name}}'
property :SetScript, 'invoke-webrequest -uri “https://download.microsoft.com/download/3/9/B/39B8DDA8-509C-4B9E-BCE9-4CD8CDC9A7DA/Exchange2016-x64.exe” -outfile “c:/Exchange2016/Exchange2016-x64.exe”'
property :TestScript,'test-path “c:/Exchange2016/Exchange2016-x64.exe”'
COMMENTED OUT property :Credential, 'Chef::Util::Powershell:PSCredential(“vagrant”, “vagrant”)'
COMMENTED OUT property :DependsOn, [File]Exchange2016Folder
end

Thank you.

Regards, Tim.


#2

Hi, so as you may have seen the first thing is that the File DSC Resource does not accept a URI location as the source for the file.

As for a remedy, there are two solutions that I have used before.

  1. The first (and my personal preferred) is to use Chef’s built-in remote_file/remote_directory resources. These resources allow you to specify a URI location and have worked well for me.

  2. The second option is to use the DSC xRemoteFile resource from the xPSDesiredStateConfiguration module.

As for using credentials in DSC Resources there is a helper method for passing PS Credentials:

ps_credential('username', 'password') 
ps_credential('password')

i.e.

  dsc_resource "xAdUser-#{username}" do
    resource :xAdUser
    property :DomainName, node['dom']['domain_fqdn']
    property :UserName, username
    property :Password, ps_credential(password)
   end

#3

Thank you @gator. Was trying to get my head around ps_credential for a while. Your example is exactly what I needed. Thanks also for the DSC File resource heads up. Will use the inbuilt Chef resources.
Thanks again.