Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

Using in a web application

Open gaborenok opened this issue 1 year ago • 12 comments

Hello Everyone!

I am working on a project where I would like to bring the DSC configurations into a web application (Basically I can change the settings on a front end side). I am open for the programing languages regarding the backend, but I just cannot figure out a way how to integrate this.

Does anyone have any ideas how can be this implemented?

Thx in advice!

gaborenok avatar Apr 14 '25 10:04 gaborenok

I didn't learn the art of developing web applications, but I've worked on a couple of web projects. I'd probably go with a ASP .NET backend because it is C#, the language PowerShell (used for the DSC configuration here) is also written in.

Create a REST-API or server-side rendering page with forms that return the changed values of the DSC configuration back to your server. There, you'd need to validate the configuration. Because PowerShell and ASP.NET are both C#, you have support for validating it using e.g. the Abstract Syntax Tree (AST). For reference on how to use it, you can see an example here: https://mikefrobbins.com/2022/04/08/use-the-abstract-syntax-tree-ast-to-inspect-powershell-command-syntax-in-scripts/ The key takeaway would be this line: $AST = [System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$null, [ref]$null), where you can input the files and get the errors and other things out of the parsing.

Behind the API or server, you need a way to store the configuration. This can be a database or a fileshare, that's up to you to decide.

Does that help you or do you have specific questions about either technology, how to implement the different parts or so?

FabienTschanz avatar Apr 14 '25 10:04 FabienTschanz

Yes this does help. We were thinking about C# in the first place also. I would like to store the configurations into a database, but I am not entirely sure how to do it.

I am focusing now mainly on Teams, Intune, and Azure AD. I am trying to do a solution where there is a baseline configuration, and for each tenant the configurations can be changed.

If you can give me one example how that should look like from the Database to the backend (C#) going to the configuration file (if that's needed) across to the DSC server, that would help a lot.

Thank you!

gaborenok avatar Apr 14 '25 10:04 gaborenok

There is an example on how to structure the files (if you were to use a directory service) in the DSC whitepaper here: https://m365dscwhitepaper.azurewebsites.net/Managing%20Microsoft%20365%20with%20Microsoft365Dsc%20and%20Azure%20DevOps.pdf

I am not a database engineer, so I can't even say how I would do it since I only know the things from a directory and files point of view. It also depends on what the users in the frontend should be able to do. Is it to modify the baseline configuration and every tenant gets that? What about changes that were done to the configuration on a per tenant basis but actually reside in the baseline configuration (so overwrite and not extend)? Should those configurations be overwritten again after the baseline configuration changes?

The easiest way would be to store a "template" in the database as a simple entry, and on a per tenant basis, if there are not configurations, you use that template. If the configurations needs to change, you could create a new entry with the updated configuration to allow reverting to a previous state if required.

Personally, I would most likely store the configuration as an entire object in the database and not split it up into properties (no idea how I would to that honestly). That way, you can fetch it in its entirety and display it in the frontend.

FabienTschanz avatar Apr 14 '25 10:04 FabienTschanz

Have you looked at NoSQL databases like MongoDB (or CosmosDB)?

bartvermeersch avatar Apr 14 '25 17:04 bartvermeersch

@gaborenok Were the answers helpful? Can we close the issue?

FabienTschanz avatar Apr 25 '25 08:04 FabienTschanz

@FabienTschanz please keep it open for a little more.

First of all thank you for your help and support on this topic!

As we are going forward with it, we have a good idea now how we want to achieve our goal, but I have more questions.

We want to separate our configuration files and I am wondering that how can we have multiple config.ps1 files and generating only one MOF file at the end? I think in the DevOps example they are doing it that way, but we cannot figure it out.

Does anyone have experience with that? Maybe some example codes?

Thank you!

gaborenok avatar Apr 30 '25 08:04 gaborenok

You can combine multiple config.ps1 files using the Join-M365DSCConfiguration command. It takes the name of the start config.ps1 file as the first argument and the directory where all of the files are as the second argument. Using that, you can join multiple config_x.ps1 files into one single config.ps1 file and then convert it to the MOF file.

# This example joins the 'M365TenantConfig.ps1' file with all the configuration files in the 'D:\testbed' directory.
PS> Join-M365DSCConfiguration -ConfigurationFile 'M365TenantConfig.ps1' -ConfigurationPath 'D:\testbed'

FabienTschanz avatar Apr 30 '25 10:04 FabienTschanz

As a follow up to the last comment, we have tried to use the Join-M365DSCConfiguration cmdlet, but it keeps throwing the: "Cannot index into a null array" error. The two config files that we tried to merge into a single one are: Config 1:

Node localhost
{
    AADUser Emergency1
    {
        UserPrincipalName      = "[email protected]"
        DisplayName            = "Emergency Account 1"
        Roles                  = @("Global Administrator")
        Password               = (ConvertTo-SecureString "12345" -AsPlainText -Force)
        Ensure                 = "Present"
        ApplicationId          = $ApplicationId
        TenantId               = $TenantId
        CertificateThumbprint  = $CertificateThumbprint
    }

    AADUser Emergency2
    {
        UserPrincipalName      = "[email protected]"
        DisplayName            = "Emergency Account 2"
        Roles                  = @("Global Administrator")
        Password               = (ConvertTo-SecureString "12345" -AsPlainText -Force)
        Ensure                 = "Present"
        ApplicationId          = $ApplicationId
        TenantId               = $TenantId
        CertificateThumbprint  = $CertificateThumbprint
    }
}

Config 2:

Node localhost
{
    AADUser Admin1
    {
        UserPrincipalName      = "[email protected]"
        Roles                  = @("Global Administrator")
        Ensure                 = "Present"
        ApplicationId          = $ApplicationId
        TenantId               = $TenantId
        CertificateThumbprint  = $CertificateThumbprint
    }

    AADUser Admin2
    {
        UserPrincipalName      = "[email protected]"
        Roles                  = @("Global Administrator")
        Ensure                 = "Present"
        ApplicationId          = $ApplicationId
        TenantId               = $TenantId
        CertificateThumbprint  = $CertificateThumbprint
    }
}

ghiurcan-david avatar May 08 '25 15:05 ghiurcan-david

@ghiurcan-david Please try either Enable-PSRemoting -Force -SkipNetworkProfileCheck or winrm quickconfig. I think that PowerShel remoting is not enabled on your system.

FabienTschanz avatar May 08 '25 15:05 FabienTschanz

I ran both commands, but the error that's being thrown didn't change. The detailed error log is: Cannot index into a null array. At C:\Program Files\WindowsPowerShell\Modules\DSCParser\2.0.0.14\Modules\DSCParser.psm1:539 char:9 $resourceType = $resource.CommandElements[0].Value ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : NullArray

Cannot index into a null array. At C:\Program Files\WindowsPowerShell\Modules\DSCParser\2.0.0.14\Modules\DSCParser.psm1:540 char:9 $resourceInstanceName = $resource.CommandElements[1].Value ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : NullArray

Cannot index into a null array. At C:\Program Files\WindowsPowerShell\Modules\DSCParser\2.0.0.14\Modules\DSCParser.psm1:553 char:35 ... foreach ($keyValuePair in $resource.CommandElements[2].KeyValuePairs) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : NullArray

Since it is a Parser error, doesn't that mean the problem is with the configuration files themselves?

ghiurcan-david avatar May 09 '25 13:05 ghiurcan-david

@ghiurcan-david It doesn't have to be a configuration file issue because it could also be because the system couldn't instantiate the objects. Please update your M365DSC version using Update-M365DSCModule in an elevated Windows PowerShell prompt, and then in a new window (also with administrative privileges), run the command again. I hope this fixes it.

FabienTschanz avatar May 10 '25 15:05 FabienTschanz

Can we close this issue?

FabienTschanz avatar Jun 04 '25 20:06 FabienTschanz