Delete DSC PowerShell Configuration

Is it possible to delete the DSC configuration from the computer after applying it?

For example, I have a configuration block as follows:

 configuration SevenZip { Script Install7Zip { GetScript = { $7ZipFolder = '{0}\7-Zip' -f $env:ProgramFiles; $Result = @{ Result = ''; } if (Test-Path -Path $7ZipFolder) { $Result.Result = 'Present'; } else { $Result.Result = 'Nonpresent'; } Write-Verbose -Message $Result.Result; $Result; } SetScript = { $7ZipUri = 'http://colocrossing.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920-x64.msi'; $OutputFile = '{0}\7-Zip.msi' -f $env:TEMP; Invoke-WebRequest -Uri $7ZipUri -OutFile $OutputFile; Write-Verbose -Message ('Finished downloading 7-Zip MSI file to {0}.' -f $OutputFile); $ArgumentList = '/package "{0}" /passive /norestart /l*v "{1}\Install 7-Zip 9.20 64-bit.log"' -f $OutputFile, $env:TEMP; $Process = Start-Process -FilePath msiexec.exe -ArgumentList $ArgumentList -Wait -PassThru; Write-Verbose -Message ('Process finished with exit code: {0}' -f $Process.ExitCode); Remove-Item -Path $OutputFile; Write-Verbose -Message ('Removed MSI file: {0}' -f $OutputFile); } TestScript = { $7ZipFolder = '{0}\7-Zip' -f $env:ProgramFiles; if (Test-Path -Path $7ZipFolder) { $true; } else { $false; } } } } 

This configuration block will generate a MOF that uses the Script resource to install the 7-Zip 9.20 64-bit on the system to which it is applied if the application is not found.

Once this configuration has been applied using the commands below, how can it be removed from the system if 7-Zip is no longer required?

 SevenZip -OutputPath c:\dsc\7-Zip; Start-DscConfiguration -Wait -Path C:\dsc\7-Zip -Verbose; 
+6
source share
6 answers

Delete-DscConfigurationDocument will do the trick https://technet.microsoft.com/en-us/library/mt143544.aspx

The Remove-DscConfigurationDocument cmdlet deletes a configuration file (.mof file) from the DSC configuration store. During configuration, the Start-DscConfiguration cmdlet copies the .mof file to a folder on the target computer. This cmdlet deletes this configuration file and performs an additional cleanup.

+6
source

To move the 7zip installation out of scope, you can apply a different configuration that does not specify 7zip. The configurations are not "rolled back", so if it was installed, it will remain installed. If it is removed later, it will not be reinstalled.

You can also follow the manual route and delete the configuration file from c: \ windows \ system32 \ configuration. You will need to remove Current.mof, backup.mof and possibly the previous .mof.

+8
source
Stephen is right. When I ran this:

Delete the item C: \ Windows \ System32 \ Configuration \ Current.mof, C: \ Windows \ System32 \ Configuration \ backup.mof, C: \ Windows \ System32 \ Configuration \ Previous.mof

Now Get-DscConfiguration returns the same error as a clean machine:

Get-DscConfiguration: Current configuration does not exist. First run the DSC configuration to create the current configuration.

Here is what I was looking for. I understand that it does not return the configuration. I just wanted to โ€œcancelโ€ the configuration used.

Thanks!

+4
source

A resource can be Present (required) or Absent (not needed) in the configuration. To subsequently cancel the configuration, you need to apply the new configuration. Script resources in DSC do not support the Ensure property, so to change the Script you need to create a modified script resource in the new AFAIK configuration.

The configuration should include TestScript , which returns false if 7zip is installed, and SetScript , which uninstalls the software. Pretty much the opposite of what you have now.

You can also change your configurations so that the Script resource downloads the msi file locally, and then has the package resource for its actual installation (or its removal), with DependsOn installed in the download script. This will be a cleaner configuration approach. The only difference between installing-config and uninstall-config is Ensure = Present vs Ensure = Absent in the Package resource, which installs / uninstalls msi.

In any case, you need to apply a new configuration, which would indicate that the software should not be installed.

If 7zip is only required during your configuration (to execute some other Script resources), perhaps you can do the following (unchecked):

 configuration ExtractFolder { Script Download7zip { GetScript = { } SetScript = { #downloads msi locally } TestScript = { } } Package Install7zip { Ensure = "Present" Path = "c:\pathToMsi\7zip.msi" Name = "7Zip" ProductId = "ACDDCDAF-80C6-41E6-A1B9-8ABD8A05027E" #7zip prodid DependsOn = [Script]Download7zip } Script RunScript{ GetScript = { } SetScript = { #unpack some file using 7zip } TestScript = { } DependsOn = [Package]Install7zip } Package Uninstall7zip { Ensure = "Absent" Path = "c:\pathToMsi\7zip.msi" Name = "7Zip" ProductId = "ACDDCDAF-80C6-41E6-A1B9-8ABD8A05027E" #7zip prodid DependsOn = [Script]RunScript } } 

If the above aprroach may be required, the ConfigurationMode settings should be set to ApplyOnly . Monitoring and automatic application are likely to be confused by two package resources that do the opposite.

+2
source

Maybe an "empty" configuration could be applied?

It sounds like DSC is a one-way street. Once you apply the configuration, you need to continue using DSC to manage this server. Installing LCM on ApplyOnly would not be a better idea, because you could at some point apply something else to this machine?

+1
source

I assume that you need to implement the uninstall script configuration yourself. I do not think you can return the configuration.

It seems to me that you should have called your configuration something like "Install-SevenZip".

By the way, thanks for this interesting illustration of how to use the script resource.

Otherwise, did you check the package resource to control the installation of your application? This may be more appropriate, even I donโ€™t know if it is possible or not to download a package from the Internet with this resource, just like you.

0
source

Source: https://habr.com/ru/post/973888/


All Articles