Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active March 12, 2018 23:32
Show Gist options
  • Save Jaykul/f6ea53699fbca188210e to your computer and use it in GitHub Desktop.
Save Jaykul/f6ea53699fbca188210e to your computer and use it in GitHub Desktop.
Get-ParseResults
function Get-ParseResults {
param(
# The script or file path to parse
[Parameter(Mandatory=$true, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[Alias("Path","PSPath")]
$Script
)
$ParseErrors = $null
$Tokens = $null
if(Test-Path "$Script" -ErrorAction SilentlyContinue) {
$AST = [System.Management.Automation.Language.Parser]::ParseFile((Convert-Path $Script), [ref]$Tokens, [ref]$ParseErrors)
} else {
$AST = [System.Management.Automation.Language.Parser]::ParseInput([String]$Script, [ref]$Tokens, [ref]$ParseErrors)
}
New-Object PSObject -Property @{
ParseErrors = $ParseErrors
Tokens = $Tokens
AST = $AST
} | % { $_.PSTypeNames.Insert(0, "System.Management.Automation.Language.ParseResults"); $_ }
}
#requires -Module poke
function New-DynamicAssembly {
[CmdletBinding()]
param(
[Parameter()]
[Alias("Name")]
[System.Reflection.AssemblyName]$AssemblyName="PowerShell",
[Version]$Version = $AssemblyName.Version,
$Path = $Pwd,
[System.Reflection.Emit.AssemblyBuilderAccess]$AssemblyBuilderAccess = "RunAndSave"
)
# We always want a version
if($PSBoundParameters.ContainsKey("Version")) {
$AssemblyName.Version = $Version
} elseif(!$AssemblyName.Version) {
$AssemblyName.Version = "1.0.0"
}
$TypeDefiner = New-TypeProxy "System.Management.Automation.Language.TypeDefiner"
$AssemblyCustomAttributes = [System.Collections.Generic.List[System.Reflection.Emit.CustomAttributeBuilder]]$TypeDefiner.GetAssemblyAttributeBuilders()
[System.AppDomain]::CurrentDomain.DefineDynamicAssembly( $AssemblyName, $AssemblyBuilderAccess, $Path, $True, $AssemblyCustomAttributes )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment