Skip to content

Commit 07a91d5

Browse files
authored
Update README.md with up-to-date example code
1 parent 418a4d3 commit 07a91d5

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

README.md

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,53 @@ Note, python-jamf can work with unsupported Jamf records, it just isn't as easy
3535

3636
This is just a quick example of the power and ease-of-use of python-jamf. The following code prints the last_contact_time from all computer records, from a computer record with the ID of 1, a computer record named "Jimmy's Mac", and computer records that match a regex. Then, it searches for a computer by name and if it exists then it changes the name. Lastly, it shows how to delete and create records.
3737

38-
import python_jamf
39-
for computer in jamf.Computers(): # Retreive the data from the server
40-
print(computer.data["general"]["last_contact_time"])
41-
42-
computers = jamf.Computers() # Use the data retrieved above, don't re-download
43-
computers.refresh() # Re-download the records from the server
44-
45-
if "1" in computers:
46-
print(computers.recordWithId(1).data['general']['last_contact_time'])
47-
48-
if "Jimmy's Mac" in computers:
49-
print(computers.recordWithName("Jimmy's Mac").data['general']['last_contact_time'])
50-
51-
for computer in computers.recordsWithRegex("J[ia]m[myes]{2}'s? Mac"): # Matches Jimmy's, James', and James's
52-
print(computer.data["general"]["last_contact_time"])
53-
54-
computer = computers.recordWithName("James's Mac)
55-
if computer:
56-
computer.refresh() # Re-download the record from the server
57-
computer.data['general']['name'] = "James' Mac"
58-
computer.save()
59-
60-
# Delete a record
61-
62-
computer = computers.recordWithName("Richard's Mac)
63-
if computer:
64-
computer.delete()
65-
66-
# Create a record (2 ways)
67-
68-
comp1 = computers.createNewRecord("computer1") # This really is a short-cut for the next line.
69-
comp2 = jamf.records.Computer(0, "computer2")
38+
```
39+
from python_jamf import server
40+
41+
jps = server.Server()
42+
43+
# Get all the computer records.
44+
computers = jps.records.Computers()
45+
46+
# for a list of all record types, see the wiki
47+
# https://github.com/univ-of-utah-marriott-library-apple/python-jamf/wiki#supported-jamf-records
48+
49+
if "test" not in computers:
50+
# Create the record (record is auto-saved)
51+
test_computer = jps.records.Computers().create({'general':{'name': 'test'}})
52+
safe_to_delete = True
53+
else:
54+
# Get the existing record
55+
results = computers.recordsWithName("test")
56+
safe_to_delete = False
57+
58+
# Note, it's possible to create computers with the same name using the API, so you
59+
# must work with multiple records
60+
if results > 0:
61+
# Just take the first one (because multiple records is probably unintended)
62+
test_computer = results[0]
63+
64+
# Change the name and then save
65+
test_computer.data["general"]["name"] = "test2"
66+
test_computer.save()
67+
68+
# Print the whole record
69+
print(test_computer.data)
70+
71+
# Search by regex
72+
for computer in computers.recordsWithRegex("tes"):
73+
print(f"{computer.data['general']['name']} has id {computer.data['general']['id']}")
74+
last_id = computer.data['general']['id']
75+
76+
if safe_to_delete:
77+
# Delete a record by id (DANGER ZONE!)
78+
last_test_computer = computers.recordWithId(last_id)
79+
if last_test_computer:
80+
print(f"Deleting id {last_id}")
81+
last_test_computer.delete() # delete is instant, no need to save
82+
else:
83+
print("This script didn't create the test record, so it's not going to delete it")
84+
```
7085

7186
A few notes. You can replace `jamf.Computers()` with `jamf.Policies()` or any supported record type.
7287

0 commit comments

Comments
 (0)