Function @ Invoke-Command

Welcome Forums General PowerShell Q&A Function @ Invoke-Command

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
5 months ago.

  • Author
    Posts
  • #146369

    Participant
    Topics: 20
    Replies: 30
    Points: 148
    Rank: Participant

    Team,

    I wrote a local function, and calling it inside invoke-command for executing it @ remote system. Also that function need a parameter called computername and I am supplying that through agr parameter. Everything works fine. But I need someyhing more. I m feeding a csv to my script, from there I m getting computer names and using foreach, I am invoking that function. That CSV file contains few static data which is also required by that function. Right now my function works fine by not showing those static data. I know that my local variable is not available @ remote pc, but I m unable to feed it properly. Following is the few portion of my script.

    
    $result = icm ${function:create-report} – argu $dc.Hostname -cn dc012 -credential $cred
    
    

    As you can see $dc is the dynamic variable from foreach. Now how can I send this variable to that remote server.

    The function was written like it will read few cells from the $dc.

    The function is creating a hashtable. Following is a sample.

    
    $tbl= @{
    
    'Hostname' = $dc.fqdn
    
    'ipv4' = $ip
    
    'Role' = $dc.role
    
    'Site' = (get-addomaincontroller -server $computername).site
    
    }
    
    

    As per the sample, that function is able to create the hashtable with ip and site value, but other two are left blank.

    Could you plz help me by guiding how I can solve that.

    Regards

    Roy.

  • #146375

    Senior Moderator
    Topics: 8
    Replies: 1008
    Points: 3,275
    Helping Hand
    Rank: Community Hero

    We need to see you function. The function should have a parameter to accept $DC object. Whatever passed to -ArgumentList parameter of Invoke-Command will be bound to Parameter of the ScriptBlock, here its the function definition.

  • #146421

    Participant
    Topics: 20
    Replies: 30
    Points: 148
    Rank: Participant

    We need to see you function. The function should have a parameter to accept $DC object. Whatever passed to -ArgumentList parameter of Invoke-Command will be bound to Parameter of the ScriptBlock, here its the function definition.

    Thanks a lot Prasoon. Your few words clear my doubt/concept. I solved it. As mentioned I did some tweaks on my function by specyfing dc variable and the default value.

    Now its working perfectly as expected..

    Again thanks a lot..

The topic ‘Function @ Invoke-Command’ is closed to new replies.