79284311

Date: 2024-12-16 10:21:08
Score: 0.5
Natty:
Report link

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
Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Joel Metsi