@@ -49,11 +49,11 @@ computers = jps.records.Computers()
4949if " test" not in computers:
5050 # Create the record (record is auto-saved)
5151 test_computer = jps.records.Computers().create({' general' :{' name' : ' test' }})
52- safe_to_delete = True # Remember that we created the record and delete it later
52+ record_to_delete = test_computer
5353else :
5454 # Get the existing record
5555 results = computers.recordsWithName(" test" )
56- safe_to_delete = False # Don't delete the record later
56+ record_to_delete = None
5757
5858 # Note, it's possible to create computers with the same name using the API, so you
5959 # must work with multiple records
@@ -73,14 +73,15 @@ for computer in computers.recordsWithRegex("tes"):
7373 print (f " { computer.data[' general' ][' name' ]} has id { computer.data[' general' ][' id' ]} " )
7474 last_id = computer.data[' general' ][' id' ]
7575
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" )
76+ # Search by ID
77+ last_result = computers.recordWithId(last_id)
78+ if last_result:
79+ print (f " { last_result.data[' general' ][' name' ]} has id { computer.data[' general' ][' id' ]} " )
80+
81+ # If this script created a record, delete it
82+ if record_to_delete:
83+ print (f " Deleting record created by this script " )
84+ record_to_delete.delete() # delete is instant, no need to save
8485```
8586
8687All supported record types are accessed like this: ` jps.records.Computers() ` , ` jps.records.Policies() ` , ` jps.records.Packages() ` , etc.
0 commit comments