PowerShell DSC is one of my favorite topics to teach, because the technology is simply amazing. Usually I do not have enough time with a customer to teach all of the built-in resources. I would guess that the Script resource is one of the least understood.This sample has tested on PSVersion 4.0.
Step 1. Script Resource Log File Example
Here is an elegantly simple use of the Script resource. We have send file contents as parameter to DSC Script. The code is its own documentation. Study it carefully.
configuration WinLogAutoGen { param( [parameter(Mandatory=$true)] [string] $Contents ) #import the module of DSC for using DSCResources. Import-DscResource -ModuleName PSDesiredStateConfiguration; # use the file resource to create file. File windowsupdatelog{ DestinationPath = 'C:\windows\DynamicParameterDemo.log'; Ensure = 'Present'; Contents = $Contents ; #'Some text lo'; } } #Create mof file WinLogAutoGen -OutputPath D:\dsc-mof #Apply mof File to Node (Server) Start-DscConfiguration -Wait -Verbose -Force -Path D:\dsc-mof
Notice the Write-Verbose statements. These are important for logging and output when the resource runs.
Step 3. Verify Output
Verify the content of DynamicParameterDemo.log file. PowerShell Command as below
Get-Content C:\Windows\DynamicParameterDemo.log
Note: We can verify the mof file content as well, PowerShell Command as below
Get-Content D:\dsc-mof\localhost.mofIMP : Target nodes(Server) can apply only one MOF file.
Reference
- Documentation: https://msdn.microsoft.com/en-us/powershell/dsc/fileresource
- http://www.informit.com/articles/article.aspx?p=2350701&seqNum=5
Your Turn
Now it is your turn. Take this sample and implement your own script resources. Leave a comment below telling what you accomplished.
Best wishes on your adventure learning DSC!
No comments:
Post a Comment