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?
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.
Thanks Will. The keyword got me in the right direction.
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
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.
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.