question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

formatting partial / dot sourced ps1 dsc scripts

See original GitHub issue

Issue Description

I’m currently creating DSC Resource modules to apply settings in our environment, I’m breaking down the actual settings to individual files to make managing the settings easier and to have an easy structure of those settings.

The issue is that the formatter is not performing the align dsc resource / hash table function on the files to import.

It’s not really relevant but i’m using the following code in my policy.schema.psm1 file

Configuration Win10CustomComputerPolicy
{
    Param(
        [Parameter(Mandatory = $false)]
        [array]
        $excludeImports = @()
    )
    
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    $importParams = @{
        Path        = $PSScriptRoot
        Exclude     = $excludeImports
        Filter      = '*.ps1'
        ErrorAction = 'SilentlyContinue'
        Recurse     = $true
    }
    $imports = @(Get-ChildItem @importParams)

    #Dot source the files
    Foreach ($import in $imports) {
        Try {
            . $import.fullname
        }
        Catch {
            Write-Error -Message "Failed to import function $($import.fullname): $_"
        }
    }
}

each individual file then looks like this:

DisableLocalMachineRunOnce.ps1

#\System\Logon\Do not process the run once list
Registry DisableLocalMachineRunOnce {
    Ensure = "Present"
    Key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Force     = $true
    ValueName = "DisableLocalMachineRunOnce"
    ValueType = "DWord"
    ValueData = "1"
}

most of the formatting works, but not the alignment, if I add a configuration section around the code it works, but that would break my import.

#\System\Logon\Do not process the run once list
configuration test {
    Registry DisableLocalMachineRunOnce {
        Ensure    = "Present"
        Key       = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
        Force     = $true
        ValueName = "DisableLocalMachineRunOnce"
        ValueType = "DWord"
        ValueData = "1"
    }
}

You can easily see the difference by creating a new .ps1 file, using the above code, the second example will auto-format the alignment in the dsc resource, the first won’t.

If you need anything else, I’m happy to provide it!

Attached Logs

I’ve not attached logs - problem seems easily reproducible, can add later if really needed.

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 10.0.17134
VSCode 1.28.1
PowerShell Extension Version 1.9.0

PowerShell Information

Name Value
PSVersion 5.1.17134.228
PSEdition Desktop
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.17134.228
BuildVersion 10.0.17134.228
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand)
Extension Author Version
asciidoctor-vscode joaompinto 0.15.1
better-comments aaron-bond 2.0.2
code-settings-sync Shan 3.1.2
code-spell-checker streetsidesoftware 1.6.10
dark-plus-material vangware 1.4.3
gitconfig sidneys1 2.0.0
markdown-all-in-one yzhang 1.6.0
markdown-checkbox PKief 1.3.0
material-icon-theme PKief 3.6.0
mssql ms-mssql 1.4.0
PowerShell ms-vscode 1.9.0
theme-bluloco-light uloco 2.6.4
theme-monokai-pro-vscode monokai 1.1.8
theme-panda tinkertrain 1.3.0
vsc-material-theme Equinusocio 2.4.2
vscode-markdownlint DavidAnson 0.20.0
vscode-mysql formulahendry 0.3.0
vscode-theme-onelight akamud 2.1.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bergmeistercommented, Nov 3, 2018

@rjmholt I answered in the PSSA repo but just FYI: The behaviour that the issue creator described is because the default of the VS Code setting of the PowerShell extension powershell.codeFormatting.alignPropertyValuePairs is set to true by default. To get the different formatting without the equals signs being aligned, one need to set the VS Code setting powershell.codeFormatting.alignPropertyValuePairs to false. This setting is a direct mapper to the AlignAssignmentStatement configuration

0reactions
rjmholtcommented, Oct 16, 2018

I’m going to close this here due to the specificity of the issue and it being handled by PSSA. If it transpires that we need to change something, we can reopen.

Read more comments on GitHub >

github_iconTop Results From Across the Web

formatting partial / dot sourced ps1 dsc scripts · Issue #1081 ... - GitHub
I'm currently creating DSC Resource modules to apply settings in our environment, I'm breaking ... formatting partial / dot sourced ps1 dsc scripts...
Read more >
Desired State Configuration Basics in Windows PowerShell
Dot sourcing occurs when you type a period (.) and then type a partial or full path to a PowerShell script. (Don't forget...
Read more >
PowerShell Constrained Language mode and the Dot-Source ...
So we are attempting to dot-source a FullLanguage script into a ConstrainedLanguage session, and that is not allowed. This is a catch 22...
Read more >
Exploring Dot-Sourcing in PowerShell - MCPmag.com
To dot source a script is incredibly simple. I'll now dot source my functions script inside of my DoStuff.ps1 script.
Read more >
The Poster of the Plethora of PowerShell Pitfalls - Simple Talk
To invoke a script in the current scope, you must dot-source it, which is simply a dot followed by a space and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found