• 1.
, , Import-Module cmdlet
Functions.ps1 . .
function Write-TextColor
{
Param(
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Object]
$Info,
[parameter(Position=1, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.ConsoleColor]
$ForegroundColor = [System.ConsoleColor]::White,
[parameter(Position=2, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Switch]
$NoNewLine
)
Process{
foreach ($value in $Info)
{
if($NoNewLine)
{
Write-Host $value -ForegroundColor $ForegroundColor -NoNewline
}
else {
Write-Host $value -ForegroundColor $ForegroundColor
}
}
}
}
function Write-InfoBlue
{
Param(
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Object]
$Information,
[parameter(Position=1, ValueFromPipeline=$true)]
[Switch]
$NoNewLine
)
Process{
Write-TextColor $Information Blue $NoNewLine
}
}
Main.ps1
Import-Module -Name "$($PSCommandPath | Split-Path)/Functions.ps1" -Force
Write-InfoBlue "Printed from imported function."

• 2.
Functions.ps1 . . , Solution1.
Main.ps1
3 .
1. Get-ScriptFunctionNames. String, .
2. Get-ScriptFunctionDefinitions. String, .
3. Get-AmalgamatedScriptFunctionDefinitions. , , Get-ScriptFunctionDefinitions.
- Powershell.
3 .
function Get-ScriptFunctionNames {
param (
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Path
)
Process{
[System.Collections.Generic.List[String]]$FX_NAMES = New-Object System.Collections.Generic.List[String]
Select-String -Path "$Path" -Pattern "function" |
ForEach-Object {
[Regex] $regexp = New-Object Regex("(function)( +)([\w-]+)")
[Match] $match = $regexp.Match("$_")
if($match.Success)
{
$FX_NAMES.Add("$($match.Groups[3])")
}
}
return $FX_NAMES.ToArray()
}
}
function Get-ScriptFunctionDefinitions {
param (
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Path
)
Process{
[System.Collections.Generic.List[String]]$FX_DEFS = New-Object System.Collections.Generic.List[String]
Get-ScriptFunctionNames -Path $Path |
ForEach-Object {
$fx = Get-Item "function:$_"
if($fx -and ($null -ne $fx))
{
$FX_DEFS.Add("function $fx { $($fx.Definition) };")
}
}
return $FX_DEFS.ToArray()
}
}
function Get-AmalgamatedScriptFunctionDefinitions {
param (
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Path
)
Process{
[System.String]$FX_DEFS = ""
Get-ScriptFunctionDefinitions -Path $Path |
ForEach-Object {
$FX_DEFS += "$_"
}
return $FX_DEFS
}
}
Write-Host
[System.String[]]$FX_NAMES = Get-ScriptFunctionNames -Path "$($PSCommandPath | Split-Path)/Functions.ps1"
[System.String[]]$FX_DEFS = Get-ScriptFunctionDefinitions -Path "$($PSCommandPath | Split-Path)/Functions.ps1"
[System.String] $FX_ALL_DEFS = Get-AmalgamatedScriptFunctionDefinitions -Path "$($PSCommandPath | Split-Path)/Functions.ps1"
. ([System.Management.Automation.ScriptBlock]::Create($FX_ALL_DEFS))
Write-InfoBlue "Printed from imported function."
Check: Dot Sourcing operator.
Dot Source
ScriptBlock class

Main.ps1, 3 .
Write-Host "• TEST 1" -ForegroundColor Magenta
$FX_NAMES |
ForEach-Object {
Write-Host $_
}
Write-Host
Write-Host "• TEST 2" -ForegroundColor Magenta
foreach($value in $FX_DEFS)
{
Write-Host $value
Write-Host "███" -ForegroundColor DarkGray
}
Write-Host
Write-Host "• TEST 3" -ForegroundColor Magenta
Write-Host $FX_ALL_DEFS

3. -
, , .
powershell , Powershell Core .
PrintColorFunctions.ps1
, 1.
Main.ps1
$FX_ALL_DEFS = Get-AmalgamatedScriptFunctionDefinitions -Path "$($PSCommandPath | Split-Path)/Functions.ps1"
$R_HOST = "192.168.211.1"
$R_USERNAME = "root"
$R_PORT = "2222"
$R_SESSION = New-PSSession -HostName $R_USERNAME@$($R_HOST):$R_PORT
Invoke-Command -ArgumentList $FX_ALL_DEFS,"Joma" -Session $R_SESSION -ScriptBlock{
Param($fxs, $name)
. ([System.Management.Automation.ScriptBlock]::Create($fxs))
Clear-Host
Write-Host "Running commands in my remote Linux Server" -ForegroundColor Green
Write-InfoBlue ($(Get-Content /etc/*-release | Select-String -Pattern "^PRETTY_NAME=.*" ).ToString().Split("=")[1])
Write-InfoBlue $(uname -a)
Write-InfoBlue $(whoami)
printf "Hello! $name"
Write-InfoBlue "Local function executed in remote context"
}
Remove-PSSession -Session $R_SESSION
