Skip to content

Instantly share code, notes, and snippets.

@Dan1el42
Created October 14, 2014 12:42
Show Gist options
  • Select an option

  • Save Dan1el42/32361edda25436a775bd to your computer and use it in GitHub Desktop.

Select an option

Save Dan1el42/32361edda25436a775bd to your computer and use it in GitHub Desktop.
Import-DataFile
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