Last active
June 29, 2020 12:41
-
-
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 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
### 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