The Environment resource in Windows PowerShell Desired State Configuration (DSC) provides a mechanism to manage system environment variables.This sample has tested on PSVersion 4.0.
Step 1. Script Environment Variable Example
Here is an elegantly simple use of the Script resource. We could make it more complex in many ways, but I prefer to start out with a simple use case first. The code is its own documentation. Study it carefully.
configuration EnvDemo{ param() #import the module of DSC for using DSCResources. Import-DscResource -ModuleName PSDesiredStateConfiguration; Environment EnvironmentExample { Ensure = "Present" # You can also set Ensure to "Absent" Name = "TestEnvironmentVariable" Value = "TestValue" } } #Set mof file path in other word where to generate mof file. EnvDemo -Outputpath D:\Harshal; #Run mof file in other word apply mof configuration to server. Start-DscConfiguration -Wait -Verbose -Force -path D:\Harshal;
Notice the Write-Verbose statements. These are important for logging and output when the resource runs.
Step 3. Verify
[Environment]::GetEnvironmentVariable("TestEnvironmentVariable",[System.EnvironmentVariableTarget]::Machine)
Reference
- Documentation:https://msdn.microsoft.com/en-us/powershell/dsc/environmentresource
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!