Example function to be saved to c:\winrm-profile.ps1
function Get-Hostname
{
return $env:COMPUTERNAME
}
To invoke normally:
$s = New-PSSession -computername remotemachine
Invoke-Command -ScriptBlock {. c:\winrm-profile.ps1; Get-Hostname} -Session $s
For a background job you need to do something like:
Invoke-Command -ScriptBlock {Start-Job {Get-Hostname} -InitializationScript {. c:\winrm-profile.ps1}} -Session $s
To check the output of the job to make sure it ran the function from the "profile":
Invoke-Command -ScriptBlock {(Get-Job).childjobs | Select Output} -Session $s
Should return something like:
Output PSComputerName RunspaceId
------ -------------- ----------
{SPVM2015} Localhost b9e22007-ce49-4e8b-8e57-986973b32be8