Powershell Remoting: using imported module cmdlets in a remote pssession

前端 未结 7 2117
我在风中等你
我在风中等你 2021-02-05 12:34

Is there a way to use modules that were imported in a local session in a remote session? I looked at import-pssession, but I don\'t know how to get the local session. Here\'s a

7条回答
  •  心在旅途
    2021-02-05 12:56

    So I was looking for something similar... In my case I just needed to export a single function to a remote session... this is what I came up with. Maybe you could loop over it to try it. It doesn't work with internal commands, but it does work on functions in custom modules (in the testing I've done).

    function Export-FunctionToSession
    {
        [CmdletBinding()]
        [Alias()]
        [OutputType([int])]
        Param
        (
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $Session,
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $FunctionName
        )
        $script = "Function $functionName(){" + (Get-Command $functionName).definition + '}'
        $scriptBlock = {Invoke-Expression $using:script}
        Invoke-Command -Session $session -ScriptBlock $scriptBlock
    }
    

提交回复
热议问题