# Invoke-Command for a Function

**URL:** <https://forums.powershell.org/t/invoke-command-for-a-function/15691>\
**Category:** PowerShell Help\
**Created:** [February 16, 2021, 3:29pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691 "2021-02-16T15:29:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [February 16, 2021, 3:29pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/1 "2021-02-16T15:29:41Z")

</div>

I am trying to get common resources from a bunch of DCs ($DCs). I can run a function (called “Get-Resources”) on a local one and the Function works. How do I structure a Function, though, with Invoke-Command?

tried this but no multiple results:

```
$DCs | % {

Invoke-Command -ComputerName $DCs -ScriptBlock { {Function:Get-Resources}}
               
}
```

---

<div class="post-metadata">

**Author:** ![ferc](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ferc/32/2135_2.png) [@ferc](https://forums.powershell.org/u/ferc)\
**Post date:** [February 16, 2021, 3:36pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/2 "2021-02-16T15:36:34Z")

</div>

If it is a custom function (written by you or someone else), then it needs to be available on each one of the servers in $DCs. If it is available, try this:

```
$DCs | % {
 
Invoke-Command -ComputerName $_ -ScriptBlock {Get-Resources}
               
}
```

---

<div class="post-metadata">

**Author:** ![ferc](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ferc/32/2135_2.png) [@ferc](https://forums.powershell.org/u/ferc)\
**Post date:** [February 16, 2021, 3:40pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/3 "2021-02-16T15:40:14Z")

</div>

I just noticed the ComputerName value is wrong as well. You are using For-EachObject, so you should use $\_. I have made the correction in my previous post.

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [February 16, 2021, 5:14pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/4 "2021-02-16T17:14:25Z")

</div>

ah, not there’s too many DCs to copy that (Function) onto. Is there another method to leverage invoke-Command to gather essentially the same info, per server (mem, cpu, at least)?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [February 19, 2021, 6:36pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/5 "2021-02-19T18:36:22Z")

</div>

You can take the contents of the function and pass it into the scriptblock as a string. Inside the scriptblock that’s running on the remote machine, you will create a new scriptblock from that string and then invoke it. If you need to pass arguments to the function then some changes would be required. Here’s a non-destructive demo you can run to test this. This assumes you have a variable that holds an array of server names

```auto
 
function say-hello {

    Write-Host Hello from $env:COMPUTERNAME
}

$servers = 'server1','server2','server3'

Invoke-Command -ComputerName $servers -ScriptBlock {
    Param($function)

    $sb = [scriptblock]::Create($function)
    $sb.Invoke()

} -ArgumentList (Get-Command say-hello).definition

```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [February 26, 2021, 5:25am UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/7 "2021-02-26T05:25:14Z")

</div>

Copied and reedited my last reply. Ooo the colors are a little harsh for ```powershell formatting.

One way, with the variable in the local scope. It’s value is a scriptblock.

```powershell
function hi { ‘hello’ }
invoke-command localhost $function:hi
hello

```

Or a script instead of a function:

```powershell
“‘hello’” | set-content hi.ps1
invoke-command localhost hi.ps1
hello

```

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [February 27, 2021, 2:30am UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/8 "2021-02-27T02:30:28Z")

</div>

That works thank you!

I have this then:

```auto
$DCs = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() |
    Select-Object -ExpandProperty Sites |
        Select-Object -ExpandProperty Servers |
            Select-Object -ExpandProperty Name |
                **Sort-Object**

function Resources{  
                 param(  
                 $computername =$env:computername  
                 )  
                 # Processor utilization 
                 #Get-WmiObject -ComputerName $computer -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object * 
                $cpu = gwmi win32_perfformatteddata_perfos_processor -ComputerName $computername| ? {$_.name -eq "_total"} | select -ExpandProperty PercentProcessorTime -ea silentlycontinue  
                 # Memory utilization 
                 $ComputerMemory = Get-WmiObject -ComputerName $computername -Class win32_operatingsystem -ErrorAction Stop 
                 $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize) 
                 $RoundMemory = [math]::Round($Memory, 2) 
                 # Free disk space 
                 $disks = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -computername $computername 
                 $results = foreach ($disk in $disks)  
                 { 
                 if ($disk.Size -gt 0) 
                 { 
                   $size = [math]::round($disk.Size/1GB, 0) 
                   $free = [math]::round($disk.FreeSpace/1GB, 0) 
                   [PSCustomObject]@{ 
                   Drive = $disk.Name 
                   Name = $disk.VolumeName 
                   "Total Disk Size" = $size 
                   "Free Disk Size" = "{0:N0} ({1:P0})" -f $free, ($free/$size) 
                   } } }     

                   # Write results 
                   Write-host "Resources on" $computername "- RAM Usage:"$RoundMemory"%, CPU:"$cpu"%, Free" $free "GB" 
                   }
Invoke-Command -ComputerName $DCs $function:Resources

```

(Notice where I put the Sort-Object. My variable has my DCs sorted but after my Invoke-Command, not really.

Is there a better place to put that Sort so my output is ordered alphabetically?

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [February 27, 2021, 2:33am UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/9 "2021-02-27T02:33:45Z")

</div>

> [@krzydoug](#):
>
> ```auto
> function say-hello {
> 
> Write-Host Hello from $env:COMPUTERNAME
> }
> 
> $servers = 'server1','server2','server3'
> 
> Invoke-Command -ComputerName $servers -ScriptBlock {
> Param($function)
> 
> $sb = [scriptblock]::Create($function)
> $sb.Invoke()
> 
> } -ArgumentList (Get-Command say-hello).definition
> 
> ```

Worked great thanks Doug

---

<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:27pm UTC](https://forums.powershell.org/t/invoke-command-for-a-function/15691/10 "2024-05-16T20:27:48Z")

</div>

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


