1

I am struggling with invoke-Command to execute custom function remotely and display the output at the local host. I've read many posts regarding invoke-Command online but I haven't find a way to resolve this problem.

First the custom function is working. It's called get-openfiles. The code is provided below:

    function get-openfiles{                                                                                  
 param(                                                                                                   
 $computername=@($env:computername),                                                                      
 $verbose=$false)                                                                                         
 $collection = @()                                                                                        
 foreach ($computer in $computername){                                                                    
 $netfile = [ADSI]"WinNT://$computer/LanmanServer"                                                        

 $netfile.Invoke("Resources") | foreach {                                                                 
 try{                                                                                                     
 $collection += New-Object PsObject -Property @{                                                          
 Id = $_.GetType().InvokeMember("Name", ‘GetProperty’, $null, $_, $null)                                  
 itemPath = $_.GetType().InvokeMember("Path", ‘GetProperty’, $null, $_, $null)                            
 UserName = $_.GetType().InvokeMember("User", ‘GetProperty’, $null, $_, $null)                            
 LockCount = $_.GetType().InvokeMember("LockCount", ‘GetProperty’, $null, $_, $null)                      
 Server = $computer                                                                                       
 }                                                                                                        
 }                                                                                                        
 catch{                                                                                                   
 if ($verbose){write-warning $error[0]}                                                                   
 }                                                                                                        
 }                                                                                                        
 }                                                                                                        
 Return $collection                                                                                       
 }

I have used following methods but none of them working. Some of example below will work for built-in function but my condition is that I have to use the custom function.

# NOT WORKING #1
$s = New-PSSession -ComputerName remote-STL
$CifServer = "fs-STL-01"
function get-openfiles{..}
invoke-command -Session $s -ScriptBlock ${function:get-openfiles} -ArgumentList $CifServer

# NOT WORKING #2
$s = New-PSSession -ComputerName remote-STL
$CifServer = "fs-STL-01"
invoke-command -Session $s -ScriptBlock {Import-Module C:\scripts\grp-functions.psm1}
invoke-command -Session $s -ScriptBlock {get-openfiles -computername $args[0]} -ArgumentList $CifServer

# Not working #3
$s = New-PSSession -ComputerName remote-STL
$CifServer = "fs-STL-01"
invoke-command -Session $s -ScriptBlock {Import-Module C:\scripts\grp-functions.psm1}
invoke-command -Session $s -ScriptBlock { param($CifServer) get-openfiles -computername $Cifserver } -ArgumentList $CifServer

# Not working #4
$CifServer = "fs-STL-01"
function get-openfiles{..}
invoke-command -ComputerName remote-STL -ScriptBlock ${function:get-openfiles} -ArgumentList $CifServe

# not working #5
$CifServer = "fs-STL-01"
invoke-command -ComputerName remote-STL -ScriptBlock {get-openfiles -computername $args[0]} -ArgumentList $CifServer

Thanks for look into it!

4
  • The two with ${function:get-openfiles} are (maybe) sending the function definition, but you don't call the function after doing that. I think you'll need to combine that with the scriptblock taking a parameter for cifserver and then calling get-openfiles Commented Sep 10, 2016 at 7:39
  • If you dropped -Session $s from your first example (and creating the session in the first place), and swapped it for -ComputerName 'fs-STL-01' it would most likely work. That syntax works for me. Commented Sep 10, 2016 at 9:08
  • Hi, Robin. Just to be clear, could you please give me the complete syntax that worked for you? I don't understand how you made swap and worked for you. Commented Sep 13, 2016 at 0:41
  • Hi, TessellatingHeckler, I would like to see your script of how to make it work. Thanks, Leon Commented Sep 13, 2016 at 0:43

1 Answer 1

2

You should send entire function in your scriptblock.

invoke-command -ComputerName computername -ScriptBlock {function get-whatever {dir}; get-whatever } -Credential (get-credential youruser)

This example runs get-whatever function that is dir command only and is the proof of concept. Change it to your desired code.

Sign up to request clarification or add additional context in comments.

1 Comment

I have applied to the proof of concept. It worked partially... which means the remote TS server took the custom function but it returned the result for the remote TS server, not the result for Cifs Server. Here is my code: $cifServer = "fs-stl-01" $remoteServer = "remote-STL" $scriptBlock = { function get-openfiles{..}; get-openfiles } invoke-command -ComputerName $remoteServer -ScriptBlock $scriptblock -ArgumentList $CifServer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.