1

We have a large number of groovy/gradle configuration files that are created by the ConfigSlurper class.

We are investigating to move our scripts over to PowerShell and quickly realized that the format is not JSON format.

I'm not finding a lot out there on the 'industry standard' configuration format. Yes there is JSON and INI... but that doesn't nearly seem as powerful as the groovy Config capabilities.

What do people use out there? Has anyone found an equivalent to the goovy/gradle config files?

thanks!

Julian
  • 137
  • 1
  • 9

1 Answers1

2

Syntax-wise, the closest equivalent is to use a .psd1 file containing a PowerShell hashtable literal, which you can read into an in-memory hashtable using Import-PowerShellDataFile.

However, note that - as of PowerShell 7.3.1 - there is no way to create such a file programmatically - GitHub issue #11300 discusses future enhancements to allow this.

This answer provides custom code for writing .psd1 files and also discusses alternative data formats, including JSON, which is also worth considering.

Example:

The PowerShell equivalent of the following Groovy config file (taken from the docs):

grails.webflow.stateless = true
smtp {
     mail.host = 'smtp.myisp.com'
     mail.auth.user = 'server'
}
resources.URL = 'http://localhost:80/resources'

is:

@{
  grails    = @{ webflow = @{ stateless = $true } }
  smtp      = @{
    mail    = @{ 
      host = 'smtp.myisp.com' 
      auth = @{ user = 'server' }
    }
  }
  resources = @{ URL = 'http://localhost:80/resources' }
}

As you can see, there are similarities in syntax, but PowerShell's is invariably more verbose, primarily because it doesn't support implicit nesting of the hasthables (maps, in Groovy terms).

Parsing the above with Import-PowerShellDataFile results in a single, nested hashtable whose top-level keys as grails, smtp, and resources - but not the the order of the entries is not guaranteed to match the order in the input file.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Wow... ya that's definitely not an option for a configuration file :) Thank you for all the feedback. I think JSON it is - we will "slurp" then save as json to migrate the conversion files i guess. Not as powerful, but may just be the way. If i don't hear of any other answers, i'll mark this as accepted. – Julian Dec 17 '22 at 01:11
  • @Julian, that sounds like a plan, given that it's easy to both read and write JSON in PowerShell, but note that JSON will be even more verbose, given that it also doesn't support nested maps (objects) and requires property names to always be double-quoted. – mklement0 Dec 17 '22 at 21:21