Skip to content

Instantly share code, notes, and snippets.

@Stephanevg
Created August 10, 2020 09:21
Show Gist options
  • Save Stephanevg/7c62b60b12fbedfb60ff4827c1c86969 to your computer and use it in GitHub Desktop.
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 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