Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Description
This example shows how you can use the Environment
resource to ensure a path environment
variable exists with a specific value.
With Ensure set to Present
, Name set to TestEnvironmentVariable
, and Value set to
TestValue
, the resource adds an environment variable called TestEnvironmentVariable
with the
value TestValue
if it doesn't exist.
With Path set to $true
, if TestEnvironmentVariable
exists and doesn't include TestValue
,
the resource appends TestValue
to the current value.
With Target set to an array with both Process
and Machine
, the resource creates or sets the
environment variable in both the process and machine targets.
With Invoke-DscResource
This script shows how you can use the Environment
resource with the Invoke-DscResource
cmdlet to
ensure TestEnvironmentVariable
is set in the process and machine targets to include TestValue
.
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Environment'
ModuleName = 'PSDscResource'
Properties = @{
Name = 'TestPathEnvironmentVariable'
Value = 'TestValue'
Ensure = 'Present'
Path = $true
Target = @(
'Process'
'Machine'
)
}
}
$NonGetProperties = @(
'Value'
'Path'
'Ensure'
)
}
process {
$TestResult = Invoke-DscResource -Method Test @SharedParameters
if ($TestResult.InDesiredState) {
$QueryParameters = $SharedParameters.Clone()
foreach ($Property in $NonGetProperties) {
$QueryParameters.Properties.Remove($Property)
}
Invoke-DscResource -Method Get @QueryParameters
} else {
Invoke-DscResource -Method Set @SharedParameters
}
}
With a Configuration
This snippet shows how you can define a Configuration
with an Environment
resource block to
ensure TestEnvironmentVariable
is set in the process and machine targets to include TestValue
.
Configuration CreatePathVariable {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Environment ExampleEnvironment {
Name = 'TestPathEnvironmentVariable'
Value = 'TestValue'
Ensure = 'Present'
Path = $true
Target = @(
'Process'
'Machine'
)
}
}
}