diff --git a/dsc/examples/file_with_condition.dsc.bicep b/dsc/examples/file_with_condition.dsc.bicep new file mode 100644 index 000000000..c01e37a29 --- /dev/null +++ b/dsc/examples/file_with_condition.dsc.bicep @@ -0,0 +1,32 @@ +// This example demonstrates how to create a file using the Windows PowerShell DSC extension. +// The file is created in the C:\DSC directory on the target machine. +// You should at least have the Bicep CLI v0.34.34 installed to run this example with experimental feature desiredStateConfiguration turned on. +// To run the second resource, you can add the --parameters '{"parameters":{"restartService":true}}' flag to the command line. +// The configuration document requires to be run elevated. +// From dsc version 3.2.0-preview.4 and above onwards, you can directly run the example using `dsc config get --file file_with_condition.dsc.bicep`. + +targetScope = 'desiredStateConfiguration' + +@description('Set to true to ensure the service is running after the file creation.') +param restartService bool = false + +resource powerShellAdapter 'PSDesiredStateConfiguration/File@2025-01-07' = { + name: 'Use Bicep to create file' + properties: { + Ensure: 'Present' + Type: 'File' + DestinationPath: 'C:\\DSC\\config.txt' + Contents: 'This file was created using Bicep extension from DSC.' + } +} + +// Optionally ensure the service is running after the file creation +resource ensureServiceRunning 'PSDesiredStateConfiguration/Service@2025-01-07' = if (restartService) { + name: 'Ensure DSC service is running' + properties: { + Name: 'Spooler' + StartupType: 'Automatic' + State: 'Running' + } +} + diff --git a/extensions/bicep/bicep.tests.ps1 b/extensions/bicep/bicep.tests.ps1 index b6ccd6d79..5de77e607 100644 --- a/extensions/bicep/bicep.tests.ps1 +++ b/extensions/bicep/bicep.tests.ps1 @@ -36,4 +36,13 @@ resource invalid 'Microsoft.DSC.Extension/Bicep:1.0' = { $content | Should -Match "Importing file '$bicepFile' with extension 'Microsoft.DSC.Extension/Bicep'" $content | Should -Match "BCP033" } + + It 'Example bicep file with condition works' -Skip:(!$foundBicep -or !$IsWindows) { + $params = @{ parameters = @{ restartService = $true } } | ConvertTo-Json -Compress + $bicepFile = Resolve-Path -Path "$PSScriptRoot\..\..\dsc\examples\file_with_condition.dsc.bicep" + $out = dsc -l trace config --parameters $params get -f $bicepFile 2>$TestDrive/error.log | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String) + $out.results[0].result.actualState.Ensure | Should -Be 'Absent' # As set is not called + $out.results[1].result.actualState.StartupType | Should -Be 'Automatic' + } }