Share via


Entfernen einer Umgebungsvariable

Beschreibung

In diesem Beispiel wird gezeigt, wie Sie die Environment Ressource verwenden können, um sicherzustellen, dass eine Umgebungsvariable ohne Pfad nicht vorhanden ist.

Mit "Set to Absent, Name set to " und Path set $falseTestEnvironmentVariableto , the resource removes the environment variable called TestEnvironmentVariable if it exists.

Mit target set to an array with both Process and Machine, the resource removes the environment variable from the process and machine targets.

Mit Invoke-DscResource

Dieses Skript zeigt, wie Sie die Environment Ressource mit dem Invoke-DscResource Cmdlet verwenden können, um sicherzustellen TestEnvironmentVariable , dass sie aus dem Prozess- und Computerziel entfernt wird.

<#
.SYNOPSIS
.DESCRIPTION
    Removes the environment variable `TestEnvironmentVariable` from both the
    machine and the process.
#>

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'Environment'
        ModuleName = 'PSDscResource'
        Properties = @{
            Name   = 'TestEnvironmentVariable'
            Ensure = 'Absent'
            Path   = $false
            Target = @(
                'Process'
                'Machine'
            )
        }
    }

    $NonGetProperties = @(
        '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
    }
}

Mit einer Konfiguration

In diesem Codeausschnitt wird gezeigt, wie Sie einen ConfigurationEnvironment Ressourcenblock definieren können, um sicherzustellen TestEnvironmentVariable , dass der Prozess und die Computerziele entfernt werden.

<#
.SYNOPSIS
.DESCRIPTION
    Removes the environment variable `TestEnvironmentVariable` from both the
    machine and the process.
#>

configuration Sample_Environment_Remove {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Environment ExampleEnvironment {
            Name   = 'TestEnvironmentVariable'
            Ensure = 'Absent'
            Path   = $false
            Target = @(
                'Process'
                'Machine'
            )
        }
    }
}