Skip to content

Add Bicep example #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions dsc/examples/file_with_condition.dsc.bicep
Original file line number Diff line number Diff line change
@@ -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'
}
}

9 changes: 9 additions & 0 deletions extensions/bicep/bicep.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Loading