This topic contains 5 replies, has 4 voices, and was last updated by
January 13, 2015 at 4:50 am #21813ParticipantPoints: 0Rank: Member
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?
January 13, 2015 at 5:21 am #21814KeymasterPoints: 0Rank: Member
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.
January 13, 2015 at 7:01 am #21823ParticipantPoints: 0Rank: Member
Thanks Will. The keyword got me in the right direction.
January 14, 2015 at 2:04 pm #21864ParticipantPoints: 0Rank: Member
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
January 14, 2015 at 5:16 pm #21866KeymasterPoints: 0Rank: Member
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.
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. 🙂
January 15, 2015 at 12:41 am #21872ParticipantPoints: 45Rank: Member
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.
The topic ‘import-module to remote session’ is closed to new replies.