Skip to content
Temporary Local User Access

Temporary Local User Access

Overview

At checkout, the broker creates (or resets) a temporary local user account on the target Windows server, adds it to a specified local group (e.g. Remote Desktop Users), and returns the username and password. At checkin, the account is deleted and any active RDP sessions can optionally be terminated.

This pattern requires no AD infrastructure — the account is local to the server.

Before You Begin

  • The broker is running and connected (see Getting Started)
  • WinRM is enabled on the target Windows server
  • The broker service account has local administrator rights on the target server

Checkout Routine

Full script: Windows/permissions/temp-local-user/checkout.ps1

Environment variables:

VariableNotes
emailInjected — requesting user’s email; username is derived by stripping the domain
groupLocal group to add the account to (e.g. Remote Desktop Users)

Derive username, generate password, create account:

$email    = $env:email
$username = ($email.Split('@')[0]) -replace '[^a-zA-Z0-9]', ''
$group    = $env:group

$chars    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
$password = -join ((0..11) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })
$securePass = ConvertTo-SecureString $password -AsPlainText -Force

$existing = Get-LocalUser -Name $username -ErrorAction SilentlyContinue
if ($existing) {
    $existing | Set-LocalUser -Password $securePass
} else {
    New-LocalUser -Name $username -Password $securePass | Out-Null
}

Add to local group and return credentials:

Add-LocalGroupMember -Group $group -Member $username

# Return credentials via response template
Write-Output "username:s:$username"
Write-Output "password:s:$password"

The username:s: and password:s: format is compatible with .rdp file syntax, making it easy to surface these as a ready-to-use RDP connection file.


Checkin Routine

Full script: Windows/permissions/temp-local-user/checkin.ps1

Environment variables:

VariableDefaultNotes
emailInjected — same as checkout
killrdp0Set to 1 to terminate active RDP sessions before deleting the account

Optionally kill RDP sessions, then delete the account:

$username = ($env:email.Split('@')[0]) -replace '[^a-zA-Z0-9]', ''
$killrdp  = if ($env:killrdp) { $env:killrdp } else { "0" }

if ($killrdp -eq "1") {
    # Terminate active RDP sessions for this user
    $sessions = (qwinsta $username 2>&1) -split "`r`n" |
        Where-Object { $_ -match "\b$username\b" }
    $sessions | ForEach-Object {
        $id = ($_ -split '\s+')[3]
        Invoke-RDUserLogoff -HostServer localhost -UnifiedSessionID $id -Force
    }
}

# Remove the local user account
Get-LocalUser -Name $username -ErrorAction SilentlyContinue |
    Remove-LocalUser -ErrorAction SilentlyContinue

Configure in Britive

Create a response template

Go to Resource Manager → Response Templates → New Template. Add fields that surface username and password from the checkout output.

Create a permission

Go to Resource Manager → Resource Type Permissions → New Permission. Set Language to PowerShell.

Paste checkout.ps1 and checkin.ps1 into the Checkout and Checkin fields.

Declare variables:

VariableSystem definedNotes
emailYesInjected automatically
groupNoe.g. Remote Desktop Users or Administrators
killrdpNo0 or 1

Under Response Templates, attach the template you created.

Create a profile

Go to Resource Manager → Profiles → New Profile. Set an expiration (e.g. 2h). Under Associations, select the resource label(s) covering the target Windows servers. Under Permissions, add the permission above.

Add a policy

Under Policies, assign members (users or tags) and configure approval or time-of-access conditions as needed.


Verify

Check out the profile

Navigate to My Access → find the profile → Check Out. Username and password appear in the response.

Connect via RDP

Open Remote Desktop Connection (mstsc.exe) and enter the target server hostname. Use the credentials from the checkout response.

Check in

Return to My AccessCheck In.

Confirm the account is removed

Get-LocalUser -Name "<username>"
# Expected: Get-LocalUser : User <username> was not found.

Troubleshoot

SymptomCauseFix
Checkout routine fails with access deniedBroker gMSA lacks local admin rights on targetAdd gMSA to target’s local Administrators group via GPO
New-LocalUser fails — user already existsPrevious checkin did not remove the accountCheck broker checkin logs; manually remove with Remove-LocalUser
RDP connection refused after checkoutAccount not in Remote Desktop UsersConfirm group variable is set to Remote Desktop Users
Account not deleted at checkinCheckin routine errorCheck broker logs; verify WinRM is reachable from broker
Invoke-RDUserLogoff not foundRunning on a non-RDS serverEnsure the Remote Desktop Services role or RSAT is installed, or set killrdp=0
Last updated on