Skip to content

Instantly share code, notes, and snippets.

@Stephanevg
Last active June 29, 2020 12:41
Show Gist options
  • Save Stephanevg/56ee103549cd9f3a40df02d9993ff72b to your computer and use it in GitHub Desktop.
Save Stephanevg/56ee103549cd9f3a40df02d9993ff72b to your computer and use it in GitHub Desktop.
create a function based on a file (Second example doesn't work due to scoping issues)
### This works
## Content of C:\Plop.ps1
Write-host "plop"
# Code to import a script file as function
$Content = Get-Content C:\plop.ps1 -raw
$sb = [ScriptBlock]::Create($F)
New-Item -Path function: -Name SayPlop -Value $sb -force
#As soon as we put this in a fucntion, it doesn't work anymore.
Function ImportScriptAs-Function {
Param(
[System.Io.fileInfo]$Path
)
$Content = Get-Content $Path.FullNAme -Raw
$sb = [ScriptBlock]::Create($Content)
New-Item -Path function: -Name SayPlop -Value $sb -force
}
ImportScriptAs-Function -Path C:\Plop.ps1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment