Is there a way to tell if function is run in remote session?

Within a function I would like to detect if function was called in remote session, ex. within Invoke-Command block.
Is that possible?

ex.

function Test-Remote
{
       # Am I called in remote session?
}

Invoke-Command -ScriptBlock { Test-Remote }

It may depend on what commands are used in the function but generally I’d think yes, has to be a way.
Comparing the PSComptuerName property returned from a remote command to the local hostname an option?

function Test-Remote
{
       # Am I called in remote session?
       $remoteVolume = Invoke-Command -ComputerName $remoteComp -ScriptBlock {Get-Volume}
       if ($remoteVolume.PSComputerName -eq $env:COMPUTERNAME) {
            Write-Output "$($remoteVolume.PSComputerName) same as $env:COMPUTERNAME (LOCAL)"      
       } else {
            Write-Output "$($remoteVolume.PSComputerName) is not same as $env:COMPUTERNAME (REMOTE)"
       }
}
$remoteComp = 'Server1'
Invoke-Command -ScriptBlock { Test-Remote }
1 Like

I can’t say exactly but I had some issues running nested Invoke-Command in the past and since then I avoid calling Invoke-Command within function if the function is already called in remote session but it’s difficult to predict how somebody else will use your function.

Another problem is that if you run Invoke-Command -ScriptBlock { Test-Remote } from your example on localhost the report will be that function is called remotely which isn’t true, and there is a chance that sample function is “invoked” on loopback.

And Get-Volume is slow but surely there are other faster commandlets or ways around this line.

I come to one similar solution with Get-PSCallStack:

Invoke-Command -ScriptBlock { if (!(Get-PSCallStack)[1]) { "remote" } } -Session $RemoteSession
Result is “remote” when Invoke-Command is called against remote host, however if called locally result is blank:
Invoke-Command -ScriptBlock { if (!(Get-PSCallStack)[1]) { "remote" } }
result is no output.

However if computer name is specified then the result is wrong:
Invoke-Command -ScriptBlock { if (!(Get-PSCallStack)[1]) { "remote" } } -ComputerName localhost
output: remote (which is false)

Get-PSCallStack is cool and faster than any other commandlet but Invoke-Command is not obviously a good solution in this case since using -ComputerName gives wrong result for loopback and also it’s not wise to call invoke-command in function that is intended to be invoked for remoting.

If the contents of a function is unknown then I don’t see how you can move forward as that’s how and where it is calculated as local or remote? Get-Volume was an example as there was no sample code posted.

I would not nest Invokes, just call function. In my testing it worked as posted and properly determined between remote and local.

I see, my bad, I added following to your example to recognize “localhost”:

if ($remoteComp -eq "localhost" -or $remoteComp -eq ".") { $remoteComp = $env:COMPUTERNAME }

So far this works, I’m not sure though how good it is to call Invoke-Command but there doesn’t seem to be any other solution.
thanks.