# ChefDK and PowerShell DSC File and Script Resource

**URL:** <https://discourse.chef.io/t/chefdk-and-powershell-dsc-file-and-script-resource/9564>\
**Category:** Chef Infra (archive)\
**Created:** [October 2, 2016, 7:47am UTC](https://discourse.chef.io/t/chefdk-and-powershell-dsc-file-and-script-resource/9564 "2016-10-02T07:47:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timhaintz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@timhaintz](https://discourse.chef.io/u/timhaintz)\
**Post date:** [October 2, 2016, 7:47am UTC](https://discourse.chef.io/t/chefdk-and-powershell-dsc-file-and-script-resource/9564/1 "2016-10-02T07:47:36Z")

</div>

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](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](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](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](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.

---

<div class="post-metadata">

**Author:** ![OxHobbs](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@OxHobbs](https://discourse.chef.io/u/OxHobbs)\
**Post date:** [October 2, 2016, 1:58pm UTC](https://discourse.chef.io/t/chefdk-and-powershell-dsc-file-and-script-resource/9564/2 "2016-10-02T13:58:43Z")

</div>

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](https://github.com/PowerShell/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
```

---

<div class="post-metadata">

**Author:** ![timhaintz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@timhaintz](https://discourse.chef.io/u/timhaintz)\
**Post date:** [October 2, 2016, 6:36pm UTC](https://discourse.chef.io/t/chefdk-and-powershell-dsc-file-and-script-resource/9564/3 "2016-10-02T18:36:14Z")

</div>

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.
