Clear-DSCData.ps1
function Clear-DSCData { <# .Synopsis Clears data stored in DSC .Description Clears data stored in DSC resource instances .Example Clear-DSCData # Clear all DSC Data .Example Clear-DSCData -ResourceName "MonitorDiskSpace" # Clear all data from the DSC resource monitor disk space .Link Get-DSCData #> [OutputType([Nullable])] [CmdletBinding(DefaultParameterSetName='AllDSCData', SupportsShouldProcess=$true, ConfirmImpact='High')] param( # The name of the DSC resource [Parameter(Mandatory=$true, Position=0, ParameterSetName='SpecificDSCData', ValueFromPipelineByPropertyName=$true)] [string] $ResourceName, # The computer name [string] $ComputerName, # The credential used to connect to the remote computer [Management.Automation.PSCredential] $Credential ) process { if ($PSCmdlet.ParameterSetName -eq 'AllDscData') { #region Find each class and call Clear-DSCData $classList = Get-WmiObject -Namespace root\Microsoft\Windows\DesiredStateConfiguration -Query "Select * From META_CLASS where __This ISA 'OMI_BaseResource'" -ErrorAction SilentlyContinue @PSBoundParameters foreach ($class in $classList) { if ('OMI_BaseResource', 'MSFT_DSCResource', 'MSFT_ResourceNotInDesiredState', 'MSFT_ResourceInDesiredState', 'MSFT_FileDirectoryConfiguration' -contains $class.Name) { continue } $PSBoundParameters['ResourceName'] = $class.Name Clear-DSCData @PSBoundParameters } #endregion Find each class and call Clear-DSCData } elseif ($PSCmdlet.ParameterSetName -eq 'SpecificDSCData') { if ($PSCmdlet.ShouldProcess("Clear $ResourceName Data")) { $WmiParams = @{ Namespace = 'root\Microsoft\Windows\DesiredStateConfiguration' Class = $ResourceName ErrorAction = 'SilentlyContinue' } if ($ComputerName) { $WmiParams += @{ComputerName = $ComputerName} } if ($Credential) { $WmiParams += @{Credential = $Credential} } $wmiInstances = Get-WmiObject @WmiParams foreach ($_ in $wmiInstances) { $_.Delete() } } } } } |