Not so elegant solution but very simple. I needed to send functions to a VM but it would be a minor tweak to send to remote session instead.
I have a custom PS module in C drive: C:\myModule.psm1
function Write-OutputString {
param(
$OutputString
)
$OutputString
}
In this example the module is sent as string to the remote session:
$ScriptOnHost = {
param(
$Module
)
# Initialize all function in module
Invoke-Expression $Module
# Call function from module
Write-OutputString -OutputString "Hello World!"
}
$VMSession = New-PSSession -VMName $VMName -Credential $VMCredentials
# Module as string
$ModuleOnHost = Get-Content "C:\myModule.psm1" -Raw
Invoke-Command -Session $VMSession -ScriptBlock $ScriptOnHost -ArgumentList $ModuleOnHost