Skip to content

Commit 33c731d

Browse files
authored
Update TRMM-create-update.ps1
1 parent c87051a commit 33c731d

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

assets/TRMM-create-update.ps1

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
<#
22
.SYNOPSIS
3-
Create or update asset from Tactical RMM to ITFlow. Uses PC serial to check if asset exists in ITFlow.
3+
Create or update asset from Tactical RMM to ITFlow. Uses MAC address to check if asset exists in ITFlow.
44
55
.REQUIREMENTS
66
- ITFlow API key.
77
- Global key in TacticalRMM named "ITFlow_API" with your ITFlow API key as the value.
88
- Global key in TacticalRMM named "ITFlow_url" with your ITFlow URL as the value.
99
- Client custom field in TacticalRMM named "ITFlow_client_ID".
10+
- Site custom field in TacticalRMM named "ITFlow_location_ID"
11+
- Site custom field in TacticalRMM named "ITFlow_network_ID"
1012
- Each client in TacticalRMM should have its ITFlow_client_ID populated with the client_id found in ITFlow.
11-
(To find the id, check the URL in ITFlow once you select a client)
13+
To find the ID, check the URL in ITFlow once you select a client.
14+
- Each client site in TacticalRMM should have ITFlow_location_ID and ITFlow_network_ID populated with the IDs.
15+
To find the IDs run this script on one PC. Assign the PC to a location and network in ITFlow. Run this script again and take note of the location and network IDs.
1216
1317
.NOTES
14-
- Uses PC serial number to check if asset exists in ITFlow.
18+
- Uses PC MAC address to check if asset exists in ITFlow.
1519
- Make sure to add the below script arguments to the script arguments section in TacticalRMM.
1620
- This script can be adapted to any RMM. TacticalRMM is only used to store the ITFlow URL, ITFlow API key, and client IDs.
1721
1822
.SCRIPT_ARGUMENTS
1923
-ITFlow_API {{global.ITFlow_API}}
2024
-ITFlow_url {{global.ITFlow_url}}
2125
-ITFlow_client_ID {{client.ITFlow_client_ID}}
26+
-ITFlow_location_ID {{site.ITFlow_location_ID}}
27+
-ITFlow_network_ID {{site.ITFlow_network_ID}}
2228
23-
.TODO
24-
- Error flags
25-
2629
.VERSION
30+
- v1.2 Changed search from serial to MAC address, added location ID and network ID
2731
- v1.1 Added verbosity, forced TLS 1.2, added exit if API read failure
2832
- v1.0 Initial Release
2933
@@ -32,14 +36,16 @@
3236
param(
3337
[string] $ITFlow_API,
3438
[string] $ITFlow_url,
35-
[string] $ITFlow_client_ID
39+
[string] $ITFlow_client_ID,
40+
[string] $ITFlow_location_ID,
41+
[string] $ITFlow_network_ID
3642
)
3743

3844
# Get PC info
3945
$asset_name = $Env:ComputerName
40-
$agent_make = (Get-WmiObject -Class:Win32_ComputerSystem).Manufacturer
41-
$agent_model = (Get-WmiObject -Class:Win32_ComputerSystem).Model
42-
$agent_serial = (Get-WmiObject -Class:Win32_BIOS).SerialNumber
46+
$asset_make = (Get-WmiObject -Class:Win32_ComputerSystem).Manufacturer
47+
$asset_model = (Get-WmiObject -Class:Win32_ComputerSystem).Model
48+
$asset_serial = (Get-WmiObject -Class:Win32_BIOS).SerialNumber
4349
$asset_os = (Get-WmiObject Win32_OperatingSystem).Caption
4450
$asset_mac = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).macaddress | Select-Object -First 1
4551
$install = ([DateTime](Get-Item -Force 'C:\System Volume Information\').CreationTime).ToString('yyyy/MM/dd')
@@ -74,33 +80,17 @@ if (Test-IsServer) {
7480
$read_module = "/api/v1/assets/read.php"
7581

7682
# Search all clients in ITFlow by serial to see if this asset already exists
77-
$uri_read = $ITFlow_url + $read_module + "?api_key=" + $ITFlow_API + "&asset_serial=" + $agent_serial
83+
$uri_read = $ITFlow_url + $read_module + "?api_key=" + $ITFlow_API + "&asset_mac=" + $asset_mac
7884

7985
# Force TLS 1.2 for this script
8086
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
8187

8288
# Check if PC exists in ITFlow database
8389
$exists = Invoke-RestMethod -Method GET -Uri $uri_read
84-
$asset_id = $exists.data.asset_id
8590

86-
# Data
87-
$body = @"
88-
{
89-
"api_key" : "$ITFlow_API",
90-
"asset_name" : "$asset_name",
91-
"asset_type" : "$asset_type",
92-
"asset_make" : "$agent_make",
93-
"asset_model" : "$agent_model",
94-
"asset_serial" : "$agent_serial",
95-
"asset_os" : "$asset_os",
96-
"asset_ip" : "$local_ip",
97-
"asset_mac" : "$asset_mac",
98-
"asset_install_date" : "$install",
99-
"asset_status" : "Deployed",
100-
"client_id" : "$ITFlow_client_ID",
101-
"asset_id" : "$asset_id"
102-
}
103-
"@
91+
$asset_id = $exists.data.asset_id
92+
$asset_location_id = $ITFlow_location_ID
93+
$asset_network_id = $ITFlow_network_ID
10494

10595
# If the asset exists update it, if not create it.
10696
if ($exists.success -eq "False") {
@@ -109,13 +99,44 @@ if ($exists.success -eq "False") {
10999
} else {
110100
if ($exists.success -eq "True") {
111101
$module = "/api/v1/assets/update.php"
102+
Write-Host "ITFlow IDs - Location ID: " $exists.data.asset_location_id "Network ID: " $exists.data.asset_network_id
103+
104+
if ( $ITFlow_location_ID -eq "0" ) {
105+
$asset_location_id = $exists.data.asset_location_id
106+
}
107+
108+
if ( $ITFlow_network_ID -eq "0" ) {
109+
$asset_network_id = $exists.data.asset_network_id
110+
}
111+
112112
Write-Host "Asset already exists - Updating..."
113113
} else {
114114
Write-Host "API connection error. Aborting..."
115115
Exit
116116
}
117117
}
118118

119+
# Data
120+
$body = @"
121+
{
122+
"api_key" : "$ITFlow_API",
123+
"asset_name" : "$asset_name",
124+
"asset_type" : "$asset_type",
125+
"asset_make" : "$assent_make",
126+
"asset_model" : "$asset_model",
127+
"asset_serial" : "$asset_serial",
128+
"asset_os" : "$asset_os",
129+
"asset_ip" : "$local_ip",
130+
"asset_mac" : "$asset_mac",
131+
"asset_install_date" : "$install",
132+
"asset_status" : "Deployed",
133+
"client_id" : "$ITFlow_client_ID",
134+
"asset_location_id" : "$asset_location_id",
135+
"asset_network_id" : "$asset_network_id",
136+
"asset_id" : "$asset_id"
137+
}
138+
"@
139+
119140
$uri_write = $ITFlow_url + $module
120141
$write = Invoke-RestMethod -Method Post -Uri $uri_write -Body $body
121142

0 commit comments

Comments
 (0)