# Make Local Function accessible to remote machine when using Invoke-Command

**URL:** <https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171>\
**Category:** PowerShell Help\
**Created:** [March 11, 2019, 2:16pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171 "2019-03-11T14:16:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 11, 2019, 2:16pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171/1 "2019-03-11T14:16:50Z")

</div>

**How can we make a Local Function accessible to a remote machine when using Invoke-Command?** I have invested hours trying to Google and use solutions others have suggested with no luck. Simply does not work for me.

**Example of what I am trying to do:**

$LogDef = function Log {

[CmdletBinding()]  
param (  
[Parameter(Mandatory= $true)][string]$str  
)

$str = “[$(Get-Date -Format “MM/dd/yyyy hh:mm:ss”)] $str”  
Write-Host “Logged: $str”  
Write-Output $str

}

$HelperScriptBlock = {  
Param( $LogDef )

. ([Scriptblock]::Create($LogDef))

Log “Current computer name is $env:COMPUTERNAME”  
}

Invoke-Command -ComputerName $computername -ArgumentList $LogDef -ScriptBlock $HelperScriptBlock

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [March 11, 2019, 3:52pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171/2 "2019-03-11T15:52:22Z")

</div>

[https://www.powershellgallery.com/packages/AZSBTools/](https://www.powershellgallery.com/packages/AZSBTools/)

```
help Export-SessionCommand -Full

NAME
    Export-SessionCommand
    
SYNOPSIS
    Function to export one or more session commands
    
    
SYNTAX
    Export-SessionCommand [-Command] <String[]> [[-ModuleName] <String>] [-Session] <PSSession> [<CommonParameters>]
    
    
DESCRIPTION
    Function to export one or more session commands
    This function takes one or more Powershell script functions/commands from current session
    and exports them to a remote PS session
    This function will ignore and not export binary functions
    Exported functions will persist on the remote computer for the user profile used with the PS remote session
    

PARAMETERS
    -Command <String[]>
        This is one or more script commands available in the current PS session
        For example Update-SmbMultichannelConnection cmdlet/function of the SmbShare PS module
        To see available script commands, you can use:
          Get-Command | ? { $_.CommandType -eq 'function' }
        
        Required? true
        Position? 1
        Default value                
        Accept pipeline input? false
        Accept wildcard characters? false
        
    -ModuleName <String>
        This is the name of the module that this function will create on the remote computer
        under the user profile of the remote PS session
        This will over-write prior existing module with the same name
        
        Required? false
        Position? 2
        Default value SBjr
        Accept pipeline input? false
        Accept wildcard characters? false
        
    -Session <PSSession>
        PSSession object usually obtained by using New-PSSession cmdlet.
        
        Required? true
        Position? 3
        Default value                
        Accept pipeline input? false
        Accept wildcard characters? false
        
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see 
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216). 
    
INPUTS
    
OUTPUTS
    The function returns a list of successfully exported commands/functions, or $false if it fails
    Example:
      CommandType Name ModuleName
      ----------- ---- ----------
      Function Get-BitLockerStatus SBjr      
      Function Get-SBDisk SBjr
    
    
NOTES
    
    
        Function by Sam Boutros
        v0.1 - 12 July 2018
    
    -------------------------- EXAMPLE 1 --------------------------
    
    PS C:\>Export-SessionCommand get-saervice,get-sbdisk,bla,get-bitlockerstatus,get-service -Session $Session -Verbose
    
    
    
    
    
    
    
RELATED LINKS
    https://superwidgets.wordpress.com/category/powershell/
```

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [March 11, 2019, 5:28pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171/3 "2019-03-11T17:28:45Z")

</div>

so your code would look like:

```
Install-Module AZSBTools -Force

function Log {

    [CmdletBinding()]
    param ([Parameter(Mandatory= $true)][string]$str )

    $str = "[$(Get-Date -Format "MM/dd/yyyy hh:mm:ss")] $str"
    Write-Host "Logged: $str"
    Write-Output $str

}

$Session = New-PSSession -ComputerName $computername
Export-SessionCommand Log -Session $Session
```

BTW, the same PS module has a Write-Log function that may do what you need from the Log function…

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 11, 2019, 6:00pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171/4 "2019-03-11T18:00:42Z")

</div>

This is a well documented thing for remote variables and functions, a quick web search using your post use case **‘powershell local function remote’** _would provide lots of articles and examples._

 

_> http://duffney.io/RunLocalFunctionsRemotely https://mikefrobbins.com/2014/03/27/run-a-local-powershell-function-against-a-remote-computer-with-powershell-remoting_

 

_See the help files on scope._

 
_```
# get function / cmdlet details
(Get-Command -Name About_Scope).Parameters
Get-help -Name About_Scope -Full
Get-help -Name About_Scope -Online
Get-help -Name About_Scope -Examples
```_
 

_Specifically …_

 

_> Example 5: Using a Local Variable in a Remote Command
> 
> ```
> To indicate that a variable in a remote command was created in the 
> local session, use the Using scope modifier. By default, Windows
> PowerShell assumes that the variables in remote commands were created
> in the remote session.
> 
> The syntax is:
> $Using:&lt;VariableName&gt;       
> 
> For example, the following commands create a $Cred variable in the
> local session and then use the $Cred variable in a remote command: 
> 
> ```_

 
_```
$Cred = Get-Credential
Invoke-Command $s {Remove-Item .\Test*.ps1 -Credential $Using:Cred}
```_
 

_> The Using scope was introduced in Windows PowerShell 3.0.
> 
> In Windows PowerShell 2.0, to indicate that a variable was created in  
> the local session, use the following command format._

 
_```
$Cred = Get-Credential
Invoke-Command $s {param($c) Remove-Item .\Test*.ps1 -Credential $c} -ArgumentList $Cred
```_

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 12, 2019, 1:09pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171/5 "2019-03-12T13:09:18Z")

</div>

Thank you all for your help and suggestions. I will try all of them out and see if I have better success.

I Googled and found a number of suggestions, yet like I said in my post, it simply was not working for me. Not sure if I was missing something or overlooking something.

I will post an update once I try what was suggested and if I find what I may have been doing wrong.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/make-local-function-accessible-to-remote-machine-when-using-invoke-command/12171/6 "2024-05-16T20:33:11Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


