Today, we are going to use “File Resource” of the “Desired State Configuration”.
If you have some standards tools or executable, which you love to keep on your every server, then this is a very good DSC resource. it is very useful for making sure, some files , folder which you want get copied.
I have a “ServerTools” folder in all of my servers, this folder contains some small handful utilities.
Today we are going to write a “Desired State Configuration Script” which create this folder on the server automatically, for achieving this obviously we are going to use our “FILE Resource”.
Syntax of the File Resource
Let’s get started.
You can see in the screenshot of my server “Posh-Demo” that there is no “ServerTools” folder exists yet.
The below screenshot is from my local share folder in my network , in which I do keep all of the Server Tools.
This is our configuration script, it’s very simple.
Type = “Directory”
Ensure = ‘Present’
Recurse = $true
SourcePath = ‘\\Server-1\Tools\ServerTools’
DestinationPath = ‘C:\ServerTools\’
Force = $true
You can see that script is very simple., You may already aware from “Configuration” and “Node” block. In file Block I have declare the following prameters.
Type = Directory # That’s means it will create a Directory, you can also set it to File, if you are copying any file.
Ensure = Present # That’s means , this folder has to be present on our server.
Recurse = $true # This will copy the root folder and it’s subfolder and files too.
SourcePath = \\Server-1\Tool\ServerTools # the Source path of the folder which you want to copy..
Destination = C:\ServerTools # Location on you configuring server, that where you want to copy folder to.
Force = $true # If the folder exists overwrite it.
Lets run the Script.
We run the script and you can see, it has exported the MOF file.
Now’ let’s start deploying it,
Start-DscConfiguration -Path .\createServerToolsFolder -Wait -Verbose
Oop! It’s not completed successfully, it give us an error about
“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. Make sure that SourcePath is a directory and that it is accessible
What happen? Why it not able to Copy the files from local share to the Server?, we run the ISE as “Administrator” it should be working! No?
The problem is ( thanks to PowerShellMagazine.com) DSC Local Configuration Manager runs as the SYSTEM account and won’t have access to network resources. We have to give permission to them.
To solve this, we have to add both, the client computer and the Sever computer account to the network share directory and give them read only access.
How to add Computer account to the share?
It’s simple, Right click on your Share Folder, Click on Properties, in Security Tab, click on edit, in Object Type , select “Computers”, click on “OK” and then in “enter the Object Name Box”, type your both computer names and add them.
And give them, Read and List Folder contents permission only.
Now, try to deploy the mof file again.
And you can see, that we didn’t get any error this time. The folder get copied successfully.
Let’s check if the “ServerTools” folder is get copies to the server.
Yes it is J
That’s all for today, I hope that you may find it useful