使用Jamf Pro API批量更新计算机库存记录管理状态

本文详细介绍了如何利用Jamf Pro API的computers-inventory-detail端点,通过编写Shell脚本批量更新Jamf Pro中Mac计算机库存记录的“受管”与“非受管”状态,并生成变更报告。

使用Jamf Pro API更新Jamf Pro计算机库存记录中的管理状态

前段时间,我写了一篇关于如何使用脚本将Jamf Pro计算机库存记录设置为受管的文章。最近,我重新审视了这个脚本,作为我整体工作的一部分,旨在更新那些仍在使用现已弃用的computers Classic API端点的脚本,使其改用Jamf Pro API的computers-inventory-detail端点。

作为这项工作的一部分,我决定不仅更新现有的用于将Jamf Pro计算机库存记录中的管理状态设置为“受管”的脚本,还编写了第二个脚本,用于将管理状态设置为“非受管”。更多详情,请参见下文。

您可以使用Jamf Pro API的computers-inventory-detail端点来设置Jamf Pro计算机库存记录中的管理状态。用于将Jamf Pro计算机库存记录中的管理状态更改为“受管”的API命令应如下所示:

1
/usr/bin/curl -H "Content-Type: application/json" "https://jamf.pro.server.here/api/v3/computers-inventory-detail/jamf_pro_computer_ID_goes_here" --header "Authorization: Bearer api_token_goes_here" -X PATCH -d '{"general":{"managed":"true"}}'

它发送以下JSON块来更新相应的计算机库存记录并更改管理状态:

1
2
3
4
5
{
  "general": {
    "managed": "true"
  }
}

用于将Jamf Pro计算机库存记录中的管理状态更改为“非受管”的API命令应如下所示:

1
/usr/bin/curl -H "Content-Type: application/json" "https://jamf.pro.server.here/api/v3/computers-inventory-detail/jamf_pro_computer_ID_goes_here" --header "Authorization: Bearer api_token_goes_here" -X PATCH -d '{"general":{"managed":"false"}}'

它发送以下JSON块来更新相应的计算机库存记录并更改管理状态:

1
2
3
4
5
{
  "general": {
    "managed": "false"
  }
}

我能够利用上面讨论的API信息创建了几个脚本,这些脚本可以:a) 更新指定计算机库存记录中的管理状态;b) 生成计算机库存记录被更新的Mac的报告。

脚本名为 Set_Jamf_Pro_Computers_To_Managed_Status.shSet_Jamf_Pro_Computers_To_Unmanaged_Status.sh,可通过以下链接获取:

这两个脚本都设计为接收一个纯文本文件中的一组Jamf Pro ID号,这些Jamf Pro ID号对应您希望更改其Jamf Pro计算机库存记录中管理状态的Mac。纯文本文件应如下所示:

1
2
3
4
5
6
416462
842736
434703
338517
481915
596669

使用这些脚本需要四个要素:

  1. 一个包含您希望删除的计算机的Jamf Pro ID的文本文件。
  2. 相应Jamf Pro服务器的URL。
  3. Jamf Pro服务器上一个具有足够权限从Jamf Pro服务器删除计算机的账户的用户名。
  4. Jamf Pro服务器上相关账户的密码。

上述引用的Jamf Pro服务器账户所需的Jamf Pro账户权限为:

  • Jamf Pro Server Objects:
    • Computers: Read, Update
    • Users: Update

一旦准备好这四个指定要素,就可以使用以下命令运行脚本:

对于 Set_Jamf_Pro_Computers_To_Managed_Status.sh

1
/path/to/Set_Jamf_Pro_Computers_To_Managed_Status.sh /path/to/plaintext_filename_here.txt

对于 Set_Jamf_Pro_Computers_To_Unmanaged_Status.sh

1
/path/to/Set_Jamf_Pro_Computers_To_Unmanaged_Status.sh /path/to/plaintext_filename_here.txt

对于 Set_Jamf_Pro_Computers_To_Managed_Status.sh,您应该看到如下输出:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
username@computername ~ % /Users/Shared/Set_Jamf_Pro_Computers_To_Managed/Set_Jamf_Pro_Computers_To_Managed_Status.sh /Users/Shared/jamfpro_computer_ids.txt
Please enter your Jamf Pro server URL : https://jamf.pro.server.here
Please enter your Jamf Pro user account : username_goes_here
Please enter the password for the username_goes_here account:
Report being generated. File location will appear below once ready.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=416462 from unmanaged to managed.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=842736 from unmanaged to managed.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=434703 from unmanaged to managed.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=338517 from unmanaged to managed.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=481915 from unmanaged to managed.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=596669 from unmanaged to managed.

Report on Macs available here: /var/folders/ps/2_yw29gj711c9d7c5w5jhyv80000gp/T/tmp.eFStgTqa5l.tsv

username@computername ~ %

作为脚本运行的一部分,将生成一份报告,并会通知您报告的存储位置。报告为TSV格式,如下所示:

Jamf Pro ID Number Make Model Serial Number UDID Computer Management Status Jamf Pro URL
416462 Apple MacBook Pro (16-inch, 2019) WD8JB8F49YS4 4D8CD419-1892-4CFE-8D18-D1DD53CC7970 Managed https://jamf.pro.server.here/computers.html?id=416462
842736 Apple MacBook Pro (16-inch, 2019) R6JG7GYVNORW 904418F7-3695-44BF-9A00-74F5CF617377 Managed https://jamf.pro.server.here/computers.html?id=842736
434703 Apple MacBook Pro (16-inch, 2019) FWPATJHWC92O 5CF418CE-3E46-481C-A171-7ACC9E1E2E7A Managed https://jamf.pro.server.here/computers.html?id=434703
338517 Apple MacBook Pro (16-inch, 2019) CVZVJ8R55467 E82C6C63-5F49-4DD3-90A0-E01EC11F6954 Managed https://jamf.pro.server.here/computers.html?id=338517
481915 Apple MacBook Pro (13-inch, M1, 2020) QL6ROPPB1SK5 CBC87B4C-28DA-417F-8790-411AF9F105AD Managed https://jamf.pro.server.here/computers.html?id=481915
596669 Apple MacBook Pro (16-inch, 2021) CNA11CBMJSNI 72811617-8C97-4EB6-BC4B-B9FA9C87B259 Managed https://jamf.pro.server.here/computers.html?id=596669

对于 Set_Jamf_Pro_Computers_To_Unmanaged_Status.sh,您应该看到如下输出:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
username@computername ~ % /Users/Shared/Set_Jamf_Pro_Computers_To_Unmanaged/Set_Jamf_Pro_Computers_To_Unmanaged_Status.sh /Users/Shared/jamfpro_computer_ids.txt
Please enter your Jamf Pro server URL : https://jamf.pro.server.here
Please enter your Jamf Pro user account : username_goes_here
Please enter the password for the username_goes_here account:
Report being generated. File location will appear below once ready.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=416462 from managed to unmanaged.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=842736 from managed to unmanaged.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=434703 from managed to unmanaged.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=338517 from managed to unmanaged.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=481915 from managed to unmanaged.

Updated management status in the computer inventory record for https://jamf.pro.server.here/computers.html?id=596669 from managed to unmanaged.

Report on Macs available here: /var/folders/ps/2_yw29gj711c9d7c5w5jhyv80000gp/T/tmp.6Ic4bx2sT8.tsv

username@computername ~ %

作为脚本运行的一部分,将生成一份报告,并会通知您报告的存储位置。报告为TSV格式,如下所示:

Jamf Pro ID Number Make Model Serial Number UDID Computer Management Status Jamf Pro URL
416462 Apple MacBook Pro (16-inch, 2019) WD8JB8F49YS4 4D8CD419-1892-4CFE-8D18-D1DD53CC7970 Unmanaged https://jamf.pro.server.here/computers.html?id=416462
842736 Apple MacBook Pro (16-inch, 2019) R6JG7GYVNORW 904418F7-3695-44BF-9A00-74F5CF617377 Unmanaged https://jamf.pro.server.here/computers.html?id=842736
434703 Apple MacBook Pro (16-inch, 2019) FWPATJHWC92O 5CF418CE-3E46-481C-A171-7ACC9E1E2E7A Unmanaged https://jamf.pro.server.here/computers.html?id=434703
338517 Apple MacBook Pro (16-inch, 2019) CVZVJ8R55467 E82C6C63-5F49-4DD3-90A0-E01EC11F6954 Unmanaged https://jamf.pro.server.here/computers.html?id=338517
481915 Apple MacBook Pro (13-inch, M1, 2020) QL6ROPPB1SK5 CBC87B4C-28DA-417F-8790-411AF9F105AD Unmanaged https://jamf.pro.server.here/computers.html?id=481915
596669 Apple MacBook Pro (16-inch, 2021) CNA11CBMJSNI 72811617-8C97-4EB6-BC4B-B9FA9C87B259 Unmanaged https://jamf.pro.server.here/computers.html?id=596669

除了上述使用用户账户进行身份验证的脚本外,GitHub上还有使用API客户端身份验证的匹配脚本,可通过上面的链接获取。如果设置一个权限有限的API客户端,以下是Jamf Pro服务器上API客户端所需的API角色权限:

  • Computers Read
  • Computers Update
  • Users Update
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计