Calling API from another computer question

Hello, I’m sorry if this is a newb question but I’m hoping to better understand how APIs are used. Specifically, how to get information about the client computer that is calling the API. For example, if I have a simple API with only a Get-ComputerInfo command, and when I call the following from a terminal on a different client PC
Invoke-RestMethod http://R2D2:5000/Get-ComputerInfo
I get the results from the host computer, not from the client as I was hoping for.

Is there something I’m misunderstanding in how this works?

Thanks!

Product: PowerShell Universal
Version: 2.6

This is expected behavior since the API is an endpoint that runs on the host machine, not the client.
An API is your interface for external systems to be able to interact with the host, but it still runs on the host by design - unless you’re specifying in your code, to run your command against a remote machine, it wouldn’t happen.

For example, you could write an API endpoint inside PSU that runs Get-ComputerInfo against another machine, if that command has a -computername parameter, or you could use invoke-command, but you would still have to pass it the hostname of the machine you want it to run against.

1 Like

Hey @frozt we use the session variables to get information about the client computer. Here are some:

  1. $Identity
  2. $UrlDefinition
  3. $RemoteIPAddress
  4. $SessionID
  5. $Method
  6. $URL
  7. $Body

You can also see what variables exist in the session by creating an API with the following code:

Get-Variable | ConvertTo-Json
1 Like

The issue here would be that Get-ComputerInfo does not have a parameter to run on a different machine. it would need to be run in a pssession or via Invoke-Command to run on the machine making the request to the API.