Created
August 10, 2020 09:21
-
-
Save Stephanevg/7c62b60b12fbedfb60ff4827c1c86969 to your computer and use it in GitHub Desktop.
Converts a script saved in a ps1 file to a function for testability.
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 | |
Function ImportScriptAs-Function { | |
Param( | |
[System.Io.fileInfo]$Path | |
) | |
$Content = Get-Content $Path.FullNAme -Raw | |
$sb = [ScriptBlock]::Create($Content) | |
New-Item -Path function: -Name $Path.BaseName -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