New-PSUEndpoint: Get identity of caller

Hello community,

I am trying to get the identity of the caller of a New-PSUEndpoint. Something like this

New-PSUEndpoint -Url "/resabc" -Endpoint {
    param(
        $User
    )  

    $UserName = ($User.Identity.Name)
    $UserName = $UserName.Split('\')[1]
    # do something with $UserName
}

I use this in a custom role:

param(
    $User
)

$UserName = ($User.Identity.Name)
$UserName = $UserName.Split('\')[1]
$IsMember = $false
$ADGroup = "MyADGroup"
$Users = Get-ADGroupMember -Identity $ADGroup -Recursive | Select SamAccountName
$Users | ForEach-Object {
    If($_.SamAccountName -eq $UserName) {
        $IsMember = $true
    }
}
$IsMember

The parameter $User works just fine there but has no effect/value when used in New-PSUEndpoint.

I’ve configured PSU with Windows Authentication outside of IIS (Security - PowerShell Universal).

My goal is to execute actions in the code of the endpoint based on the user identity. I do not want the users to have to somehow provide their username. It should come with a request like this

Invoke-RestMethod http://servername.xy.com:5000/resabc

I’m assuming there is a simple paramater/variable/whatever to get the caller identity? I couldn’t find anything in this forum.

Thanks for any help on that!

Product: PowerShell Universal
Version: 1.5.13

Hello @sandro I believe you are looking for the built in variable $user this is documented here:- Building Dashboards - PowerShell Universal and I have used this to greet users, and log to a log file on who actually visits the dashboard. I hope this helps

Hello @psDevUK
Thank you for the hint. Just tried, I get AD domain and user of the logged in user when I use $user in a dashboard (i.e. New-UDDashboard). Unfortunately it does not seem to work with API endpoints (i.e. New-PSUEndpopint). So I’m assuming this variable is not supported (yet) in APIs?

This was a miss on my part. I can get this added. I’ll open an issue for it.

2 Likes

I realized that we are passing this in but as $Identity. I’ll fix this so it’s consist and passes the same variable name as dashboards ($UserName).

1 Like

Great! Just successfully tested this. Having it consistent later is of course welcome too.