How to import custom PowerShell module into the remote session?

前端 未结 5 1295
名媛妹妹
名媛妹妹 2020-12-13 02:18

I\'m developing a custom PowerShell module, which I\'d like to use in context of a remote session to a different computer. The following code (which obviously doesn\'t work)

5条回答
  •  星月不相逢
    2020-12-13 02:52

    I don't believe this is supported right of the box without any "hacks". The smart move would probably be to put the module on a public location like a fileserver and import it on the server when you need it. Ex:

    $session = new-pssession -computerName server01
    invoke-command -session $session -scriptblock {
        #Set executionpolicy to bypass warnings IN THIS SESSION ONLY
        Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
        #Import module from public location
        Import-Module \\fileserver\folders\modulelocation...
    
    
        <# use function defined in MyCustomModule here #> 
    }
    

提交回复
热议问题