Created
October 14, 2014 12:42
-
-
Save Dan1el42/32361edda25436a775bd to your computer and use it in GitHub Desktop.
Import-DataFile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Import-DataFile | |
| { | |
| <# | |
| Posted by Dave Wyatt at http://powershell.org/wp/forums/topic/how-to-store-hashtable-in-configuration-file/ | |
| #> | |
| param ( | |
| [Parameter(Mandatory)] | |
| [string] $Path | |
| ) | |
| try | |
| { | |
| $content = Get-Content -Path $path -Raw -ErrorAction Stop | |
| $scriptBlock = [scriptblock]::Create($content) | |
| # This list of approved cmdlets and variables is what is used when you import a module manifest | |
| [string[]] $allowedCommands = @( | |
| 'Import-LocalizedData', 'ConvertFrom-StringData', 'Write-Host', 'Out-Host', 'Join-Path' | |
| ) | |
| [string[]] $allowedVariables = @('PSScriptRoot') | |
| # This is the important line; it makes sure that your file is safe to run before you invoke it. | |
| # This protects you from injection attacks / etc, if someone has placed malicious content into | |
| # the data file. | |
| $scriptBlock.CheckRestrictedLanguage($allowedCommands, $allowedVariables, $true) | |
| return & $scriptBlock | |
| } | |
| catch | |
| { | |
| throw | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment