GithubHelp home page GithubHelp logo

microsoft / reversedsc Goto Github PK

View Code? Open in Web Editor NEW
140.0 26.0 43.0 125 KB

Utility Library that Handles the Extraction of a DSC configuration script from an existing environment

License: MIT License

PowerShell 100.00%

reversedsc's Introduction

As part of effort to isolate the ReverseDSC Core to allow the community to build additional projects around the module, this repository is now solely focused on managing the ReverseDSC Core.

reversedsc's People

Contributors

bjoernf73 avatar gerane avatar microsoft-github-policy-service[bot] avatar microsoftopensource avatar msftgits avatar nikcharlebois avatar ricmestre avatar salbeck-sit avatar skoomasteve avatar tonylea avatar william-francillette avatar yagmurs avatar ykuijs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reversedsc's Issues

Installing 2.0.0.0 from PowerShell gallery

Attempting to upgrade from 1.9.4.4 to 2.0.0.0 and not yet finding success using commands such as:

Update-Module -Name ReverseDSC -Force
also tried
Install-Module -Name ReverseDSC -AllowClobber -Force

Please advise to help resolve and thank you.

ReverseDSC Deltas

We would like to use ReverseDSC to help newcomers to DSC get a leg up on setting up their service. Primary use case would be to run ReverseDSC on a new, unconfigured system with no reference DSC to get ALL of the configurations. Then the admin sets the system up in the 'old' way and re-runs ReverseDSC. Then I could take the 2 output files and find the deltas, which should contain the DSC specific to setting up their service with the settings they need, and gives them a way to start 85% to the finish line.

Get-DSCBlock doesn't properly handle arrays of CIMInstance

If the parameter passed to the Get-DSCBlock function is an array of CIMInstance, the the output is just a sequential string of all instances. No @() and commas between. This is causing some issues to certain ReverseDSC Orchestrator Scripts (e.g. Web site bindings in the the xWebSite resource of the xWebAdministration Module).

Export-TargetResource is not exported?

Is it something I'm doing or is the Export-TargetResource cmdlet no longer available?

When I go back as far as 1.9.2.6 it is there but at some point it seems to have dropped off

CommandType Name Version Source


Function Add-ConfigurationDataEntry 2.0.0.4 ReverseDSC
Function Add-ReverseDSCUserName 2.0.0.4 ReverseDSC
Function Convert-DSCStringParamToVariable 2.0.0.4 ReverseDSC
Function Get-ConfigurationDataContent 2.0.0.4 ReverseDSC
Function Get-ConfigurationDataEntry 2.0.0.4 ReverseDSC
Function Get-Credentials 2.0.0.4 ReverseDSC
Function Get-DSCBlock 2.0.0.4 ReverseDSC
Function Get-DSCDependsOnBlock 2.0.0.4 ReverseDSC
Function Get-DSCFakeParameters 2.0.0.4 ReverseDSC
Function Get-DSCParamType 2.0.0.4 ReverseDSC
Function New-ConfigurationDataDocument 2.0.0.4 ReverseDSC
Function Resolve-Credentials 2.0.0.4 ReverseDSC
Function Save-Credentials 2.0.0.4 ReverseDSC
Function Test-Credentials 2.0.0.4 ReverseDSC

CommandType Name Version Source


Function Add-ConfigurationDataEntry 2.0.0.3 ReverseDSC
Function Add-ReverseDSCUserName 2.0.0.3 ReverseDSC
Function Convert-DSCStringParamToVariable 2.0.0.3 ReverseDSC
Function Get-ConfigurationDataContent 2.0.0.3 ReverseDSC
Function Get-ConfigurationDataEntry 2.0.0.3 ReverseDSC
Function Get-Credentials 2.0.0.3 ReverseDSC
Function Get-DSCBlock 2.0.0.3 ReverseDSC
Function Get-DSCDependsOnBlock 2.0.0.3 ReverseDSC
Function Get-DSCFakeParameters 2.0.0.3 ReverseDSC
Function Get-DSCParamType 2.0.0.3 ReverseDSC
Function New-ConfigurationDataDocument 2.0.0.3 ReverseDSC
Function Resolve-Credentials 2.0.0.3 ReverseDSC
Function Save-Credentials 2.0.0.3 ReverseDSC
Function Test-Credentials 2.0.0.3 ReverseDSC

CommandType Name Version Source


Function Add-ConfigurationDataEntry 1.9.2.6 ReverseDSC
Function Add-ReverseDSCUserName 1.9.2.6 ReverseDSC
Function Convert-DSCStringParamToVariable 1.9.2.6 ReverseDSC
Function Export-TargetResource 1.9.2.6 ReverseDSC
Function Get-ConfigurationDataContent 1.9.2.6 ReverseDSC
Function Get-ConfigurationDataEntry 1.9.2.6 ReverseDSC
Function Get-Credentials 1.9.2.6 ReverseDSC
Function Get-DSCBlock 1.9.2.6 ReverseDSC
Function Get-DSCDependsOnBlock 1.9.2.6 ReverseDSC
Function Get-DSCFakeParameters 1.9.2.6 ReverseDSC
Function Get-DSCParamType 1.9.2.6 ReverseDSC
Function Get-ResourceFriendlyName 1.9.2.6 ReverseDSC
Function New-ConfigurationDataDocument 1.9.2.6 ReverseDSC
Function Resolve-Credentials 1.9.2.6 ReverseDSC
Function Save-Credentials 1.9.2.6 ReverseDSC
Function Test-Credentials 1.9.2.6 ReverseDSC

Get-DSCParamType can return an array instead of a string

Get-DSCParamType uses ForEach-Object loops. ForEach-Object doesn't break/end the function when return is called because it gets the next item from the pipeline. Under the right circumstances, an array will be returned. Subsequently, get-dscBlock will fail to work properly as the returned paramType won't match as intended.

How to reproduce:

$DSCModule = Get-Module -Name xWebAdministration -ListAvailable
$DSCPath = $DSCModule | Select-Object -ExpandProperty modulebase 
$modulePath = $DSCPath + "\DSCResources\MSFT_xWebsite\MSFT_xWebsite.psm1"

$paramType = Get-DSCParamType -ModulePath $ModulePath -ParamName "`$BindingInfo"

"Microsoft.Management.Infrastructure.CimInstance[]" -eq $paramType #Returns False 

"Microsoft.Management.Infrastructure.CimInstance[]" -eq $paramType[0] #Returns True 

#$paramType is an array which equals @("Microsoft.Management.Infrastructure.CimInstance[]", "")

To Resolve: (use foreach instead of foreach-object)

function Get-DSCParamType
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)] [System.String] $ModulePath,
        [parameter(Mandatory = $true)] [System.String] $ParamName
    )

    $tokens = $null
    $errors = $null
    $ast = [System.Management.Automation.Language.Parser]::ParseFile($ModulePath, [ref] $tokens, [ref] $errors)
    $functions = $ast.FindAll( {$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)

    ForEach($function in $functions){
        if ($function.Name -eq "Set-TargetResource")
        {
            $functionAst = [System.Management.Automation.Language.Parser]::ParseInput($function.Body, [ref] $tokens, [ref] $errors)

            $parameters = $functionAst.FindAll( {$args[0] -is [System.Management.Automation.Language.ParameterAst]}, $true)
            ForEach($parameter in $parameters){
                if ($parameter.Name.Extent.Text -eq $ParamName)
                {
                    $attributes = $parameter.Attributes
                    ForEach($attribute in $attributes){
                        if ($attribute.TypeName.FullName -like "System.*")
                        {
                            return $attribute.TypeName.FullName
                        }
                        elseif ($attribute.TypeName.FullName.ToLower() -eq "microsoft.management.infrastructure.ciminstance")
                        {
                            return "System.Collections.Hashtable"
                        }
                        elseif ($attribute.TypeName.FullName.ToLower() -eq "string")
                        {
                            return "System.String"
                        }
                        elseif ($attribute.TypeName.FullName.ToLower() -eq "boolean")
                        {
                            return "System.Boolean"
                        }
                        elseif ($attribute.TypeName.FullName.ToLower() -eq "bool")
                        {
                            return "System.Boolean"
                        }
                        elseif ($attribute.TypeName.FullName.ToLower() -eq "string[]")
                        {
                            return "System.String[]"
                        }
                        elseif ($attribute.TypeName.FullName.ToLower() -eq "microsoft.management.infrastructure.ciminstance[]")
                        {
                            return "Microsoft.Management.Infrastructure.CimInstance[]"
                        }
                    }
                }
            }
        }
     }
 }

Convert-DSCStringParamToVariable: throws exception if `$ParameterName` not found in $DscBlock

Description of Problem

As an example, in Microsoft365DSC, resource PlannerTask, function Export-TargetResource, line 787:

$content = Convert-DSCStringParamToVariable -DSCBlock $content `
    -ParameterName 'Credential'

The function is called to ensure that quotes are removed around the values that are variables and not strings, i.e. instead of the following which does not work in quotes:

PlannerTask 7ad38863-ce48-4b8e-b169-f292222471e6
{
	...
	Credential = "$ConfigurationData.NonNodeData.Credential"
        ...
}

we remove the quote, so the variable sis actually resolved:

PlannerTask 7ad38863-ce48-4b8e-b169-f292222471e6
{
	...
	Credential = $ConfigurationData.NonNodeData.TenantId
        ...
}

However, the presence of Credential is dependent on if Credential was used to authenticate, so it may not exist - ApplicationId with CertificateThumbprint may for instance be used instead. If so, the function Convert-DSCStringParamToVariable throws an exception when looking for Credential on the line below # MY COMMENT:

$startPosition = -1
do
{
    $startPosition = $DSCBlock.IndexOf(' ' + $ParameterName + ' ', $startPosition + 1)
    # MY COMMENT: the lines below fails if $ParameterName is not found i.e. $startPosition is still -1, since you cannot start at position -1
    $testValidStartPositionEqual = $DSCBlock.IndexOf("=", $startPosition)
    $testValidStartPositionQuotes = $DSCBlock.IndexOf("`"", $startPosition)
   
} while ($testValidStartPositionEqual -gt $testValidStartPositionQuotes -and
    $startPosition -ne -1)

The exception:

System.Management.Automation.MethodInvocationException: Exception calling "IndexOf" with "2" argument(s): "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex" ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex
   at System.String.IndexOf(String value, Int32 startIndex, Int32 count, StringComparison comparisonType)
   at CallSite.Target(Closure , CallSite , String , String , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at Convert-DSCStringParamToVariable(Closure , FunctionContext )
   at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   at System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
   at System.Management.Automation.CommandProcessorBase.Complete()
"Error during Export:"
at Convert-DSCStringParamToVariable, C:\Modules\ReverseDSC\2.0.0.13\ReverseDSC.Core.psm1: line 688
at Export-TargetResource, C:\Modules\Microsoft365DSC\DSCResources\MSFT_PlannerTask\MSFT_PlannerTask.psm1: line 787
...

Suggested solution

If $ParameterName is not found, return the original $DSCBlock:

$startPosition = -1
do
{
    $startPosition = $DSCBlock.IndexOf(' ' + $ParameterName + ' ', $startPosition + 1)
    # If the ParameterName is not found, $startPosition is still -1, and .IndexOf($string, $startPosition) does not work, so skip
    if ($startPosition -ne -1)
    {
        $testValidStartPositionEqual = $DSCBlock.IndexOf("=", $startPosition)
        $testValidStartPositionQuotes = $DSCBlock.IndexOf("`"", $startPosition)
    }
} while ($testValidStartPositionEqual -gt $testValidStartPositionQuotes -and
    $startPosition -ne -1)

# If $ParameterName was not found i.e. $startPosition is still -1, skip this section as well. We 
# just want the original $DSCBlock to be returned without modification
if ($startPosition -ne -1) {
    $endOfLinePosition = $DSCBlock.IndexOf(";`r`n", $startPosition)
    
    if ($endOfLinePosition -eq -1)
    {
        $endOfLinePosition = $DSCBlock.Length
    }
    $startPosition = $DSCBlock.IndexOf("`"", $startPosition)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.