# Use function in remote session, return value to local session?

**URL:** <https://forums.powershell.org/t/use-function-in-remote-session-return-value-to-local-session/4085>\
**Category:** PowerShell Help\
**Created:** [April 22, 2015, 7:07am UTC](https://forums.powershell.org/t/use-function-in-remote-session-return-value-to-local-session/4085 "2015-04-22T07:07:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason-mcmahan](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jason-mcmahan](https://forums.powershell.org/u/jason-mcmahan)\
**Post date:** [April 22, 2015, 7:07am UTC](https://forums.powershell.org/t/use-function-in-remote-session-return-value-to-local-session/4085/1 "2015-04-22T07:07:56Z")

</div>

I had an interesting problem today I was not quite sure how to address.  
I am manipulating certificates in the localmachine store on a remote server and would like to use a function from the local session within that remote session.  
I am using write-processlog to create a log of the commands as they are executed.

```
Function write-Processlog
{	param([string]$logstring)
    Write-Host $logstring
	Add-Content -LiteralPath $certCreationLog $logstring
}
```

Then I thought I would like to use this function with in my invoke-command block to return the certificates I remove.

```
Invoke-Command -ComputerName $FQDN -ScriptBlock {
        param ($FQDN) $certs = Get-ChildItem cert:/localmachine/my | Where-Object {$_.Subject -notmatch $FQDN}
            foreach($cert in $certs){
                $store = Get-Item $cert.PSParentPath
                $store.Open('ReadWrite')
                $store.Remove($cert)
                $store.Close()
            }
        } -ArgumentList $FQDN -ErrorAction Stop
```

Any help returning $cert that is removed would be helpful.

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [April 22, 2015, 8:21am UTC](https://forums.powershell.org/t/use-function-in-remote-session-return-value-to-local-session/4085/2 "2015-04-22T08:21:53Z")

</div>

Hi Jason,

Returning objects from a remote computer is really easy.

```
$RemovedCertInformationList = Invoke-Command -ComputerName $FQDN -ScriptBlock {
        param ($FQDN) $certs = Get-ChildItem cert:/localmachine/my | Where-Object {$_.Subject -notmatch $FQDN}
            foreach($cert in $certs){
                $store = Get-Item $cert.PSParentPath
                $store.Open('ReadWrite')
                $store.Remove($cert)
                $store.Close()

                Write-Output -InputObject $cert # Output certificate object to the pipeline on the remote computer
            }
        } -ArgumentList $FQDN -ErrorAction Stop
```

Best,  
Daniel

---

<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:45pm UTC](https://forums.powershell.org/t/use-function-in-remote-session-return-value-to-local-session/4085/3 "2024-05-16T20:45:28Z")

</div>

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


