import-module to remote session

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Tim Pringle Tim Pringle 1 year, 10 months ago.

  • Author
    Posts
  • #21813
    Profile photo of Jarek C.
    Jarek C.
    Participant

    I created a script that works on a local host relying on a custom module from codeplex. I turns out I need to run my script against a remote host as and when. Installing the module on remote hosts is out of question as they cannot store any extra software so I'm a bit stuck since I can't see a way of using the module remotely. Is there a workaround?

  • #21814
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    You should be able to import the module on a remote share if you use CredSSP. Otherwise, you'll be prevented from accessing the remote resource due to the double-hop issue.

  • #21823
    Profile photo of Jarek C.
    Jarek C.
    Participant

    Thanks Will. The keyword got me in the right direction.

  • #21864
    Profile photo of H Man
    H Man
    Participant

    you can import a module form a remote machine like this

    $dc = New-PSSession -ComputerName flldc01
    import-module -name ActiveDirectory -PSSession $dc
    

    or you can run a function on remote machine

    $grb = (get-item Function:\Get-RecycleBin).scriptblock
    invoke-command -ScriptBlock $grb -ComputerName $computers -Credential globomantics\jeff -hidecomputername | Sort Sum –Descending | Format-Table Sum,Count,Computername -Autosize
    
  • #21866
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    For a module that's installed on the remote machine, you'd be correct HMan. Unfortunately, in his original post he mentioned that he can't install the module locally, therefore he'd have to access the module remotely from the remote machine. Thus he'll run into the double-hop issue even from the PSSession.

    YourLocalComputer ->RemoteComputer->RemoteComputerWithModule

    He'll get an error attempting to access the remote resources. PowerShell.org has an excellent free resource, Secrets of PowerShell Remoting. It's a really good read. đŸ™‚

  • #21872
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    If don't mind a bit of dirty coding, you could do something like the following, which I've used from time to time. It's really pretty gross, so get the sickbag ready.

    Use get-content to read the module into a string variable (you may need to use $variablename = get-content | out-string to maintain formatting)
    Use get-content to read your script into a string variable (as above re formatting)
    Use invoke-command with the -computername parameter, and also using -scriptblock
    In the script block, refer to the variables via $using:variablename, and write them to a temporary folder via Set-Content (you may need to use $variablename | out-string | set-content to retain formatting)
    Dot source the module file you've created in the temp folder
    Run the script file you've created in the temp folder
    Remove the files you've created in the temp folder

    For a completely non file writing option, you should actually be able to use Invoke-Expression within the scriptblock without the need to write the files to disk, but it's something I've not yet tried.

You must be logged in to reply to this topic.