# Function @ Invoke-Command

**URL:** <https://forums.powershell.org/t/function-invoke-command/12254>\
**Category:** PowerShell Help\
**Created:** [March 24, 2019, 4:39am UTC](https://forums.powershell.org/t/function-invoke-command/12254 "2019-03-24T04:39:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![j2sankha](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@j2sankha](https://forums.powershell.org/u/j2sankha)\
**Post date:** [March 24, 2019, 4:39am UTC](https://forums.powershell.org/t/function-invoke-command/12254/1 "2019-03-24T04:39:04Z")

</div>

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.

[pre]

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

[/pre]

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.

[pre]

$tbl= @{

‘Hostname’ = $dc.fqdn

‘ipv4’ = $ip

‘Role’ = $dc.role

‘Site’ = (get-addomaincontroller -server $computername).site

}

[/pre]

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.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [March 24, 2019, 6:59am UTC](https://forums.powershell.org/t/function-invoke-command/12254/2 "2019-03-24T06:59:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![j2sankha](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@j2sankha](https://forums.powershell.org/u/j2sankha)\
**Post date:** [March 24, 2019, 9:09am UTC](https://forums.powershell.org/t/function-invoke-command/12254/3 "2019-03-24T09:09:56Z")

</div>

[quote quote=146375]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.

[/quote]  
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…

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/function-invoke-command/12254/4 "2024-05-16T20:33:07Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


