You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Moves an Windows Workstation Agent to the first Site created for the Client with a match in the Client's "Domains" Custom Field.
4
+
5
+
Assumptions made:
6
+
- The User will be on a Windows client machine and logged in with their Microsoft 365 account, therefore their UPN will be their M365 username (UPN)
7
+
- The lowest value ID site per Client is the one you want the Agent to be pushed to (or Clients only have 1 site)
8
+
- - If this is not true, you'll need to update the "Determine the Site..." region with logic that returns the desired site(s)
9
+
- There is a Client Custom Field called "Domains" and it's for all of the the domains that could be valid for the UPN
10
+
11
+
You will need:
12
+
- A script in TRMM that runs "whoami /upn" and the ID of that script (hover over it) - E.g. "Win_TRMM_GetLoggedOnUPN.ps1"
13
+
- - The ID will be the value supplied to the "-whoAmIScriptId" script arguement
14
+
- - This is deliberately not done as a script snippit because it must be run as the USER (and not SYSTEM)
15
+
- A Client Custom Field for storing domains exists, and is populated with the domains that any valid UPN may have for the given Client
16
+
- - The name of this field can be overridden by supplying the "-clientCustomFieldName" script argument with a value
17
+
18
+
.SYNOPSIS
19
+
Moves an Agent to the first Site created for the Client with a match in the Client's "Domains" Custom Field. (Assumes Windows & the user is logged in with their M365 account, and requires an additional script (see Description).)
20
+
21
+
.NOTES
22
+
v1.0 2026-01-12 Owen Conti
23
+
24
+
#>
25
+
26
+
[cmdletbinding()]
27
+
Param(
28
+
[string]#Provided by using the script arguement "-agentId {{agent.agent_id}}"
29
+
$agentId,
30
+
[string]#The API Key value to use. A better approach is to pass this with an ENVIRONMENT VARIABLE in the script triggering so your API key is not logged in the Windows Event Log on all the Clients that run this. If you provide a value here it will override any environment variable you provide.
31
+
$apiKey,
32
+
[int]#The ID of the "WhoAmI" script on your environment
33
+
$whoAmIScriptId,
34
+
[string]#The FQDN of your API end point (the URI is built later). E.g. "api.example.com"
35
+
$apiFQDN,
36
+
[string]#The exact name of the Client level Custom Field used for storing the valid Domains
37
+
$clientCustomFieldName="Domains"
38
+
)
39
+
40
+
#region script wide parameters
41
+
If($apiKey){
42
+
$apiAuthKey=$apiKey
43
+
} else {
44
+
$apiAuthKey=$env:apiKey
45
+
}
46
+
47
+
$headers=@{
48
+
'Content-Type'='application/json'
49
+
'X-API-KEY'=$apiAuthKey
50
+
} #These are common to all our API calls
51
+
#endRegion
52
+
53
+
54
+
#region Collect the User's UPN using the "Who Am I" script
55
+
$whoamiPayload=@{
56
+
output="wait"
57
+
emails=@()
58
+
emailMode="default"
59
+
custom_field=$null
60
+
save_all_output=$false
61
+
script=$whoAmIScriptId
62
+
args=@()
63
+
env_vars=@()
64
+
run_as_user=$false
65
+
timeout=10
66
+
} |ConvertTo-Json
67
+
68
+
$upn=Invoke-RestMethod-Uri "https://$apiFQDN/agents/$agentId/runscript/"-Method POST -Body $whoamiPayload-Headers $headers
69
+
If($upn.Contains("ERROR:"))
70
+
{
71
+
#The user is not logged in with an account that presents the UPN to whoami.exe
72
+
Write-Error"An error occured when trying to collect the UPN of the user: $upn"
73
+
Exit1
74
+
}
75
+
#endRegion
76
+
77
+
#region Determine the Site (and therefore Client) the user belongs to
78
+
$domain=$upn.Split("@")[1].Trim() #The trim is required as trailing spaces cause problems
79
+
#Need to define a method for doing a lookup for UPN domain to Customer ID to correct Site in TRMM
80
+
$customFields=Invoke-RestMethod-Uri "https://$apiFQDN/core/customfields"-Method GET -Headers $headers
81
+
$domainFieldId=$customFields|Where-Object-FilterScript {$_.model-eq"client"-and$_.name-eq$clientCustomFieldName} |Select-Object-ExpandProperty id
82
+
83
+
$clients=Invoke-RestMethod-Uri "https://$apiFQDN/clients/"-Method GET -Headers $headers
$moveResult=Invoke-RestMethod-Method PUT -Uri "https://$apiFQDN/agents/$agentId"-Headers $headers-Body $body
99
+
If($moveResult-eq"The agent was updated successfully")
100
+
{
101
+
$moveResult
102
+
Exit0
103
+
} else {
104
+
Write-Error"There was a problem with the API call to move the Agent"
105
+
$moveResult
106
+
Exit1
107
+
}
108
+
109
+
} else {
110
+
Write-Error"There was a problem collecting the Site ID. The domain value is between the asterisks: ***$domain*** Check the Clients and ensure this domain value is in the correct place."
0 commit comments