Create Smart Computergroup via API

tep
Contributor II

Hi all,

I'm trying to create a smart group across multiple JSS contexts via the rest API, and not having much luck. Basically, I want smart group that contains computers which haven't checked in over 50 days. I'm using: curl -skfu username:password https://jssaddress:8443/JSSResource/computergroups/id/0 -T /pathtofile/NoCheckin.xml -X POST;

Here's my xml:
<?xml version="1.0" encoding="UTF-8"?><computer_group><name>No check-in</name><is_smart>true</is_smart><criteria><size>1</size><criterion><name>Last Check-in</name><priority>0</priority><and_or>and</and_or><search_type>more than x days ago</search_type><value>50</value></criterion></criteria><computers></computer_group>

I've used this technique to import policies, packages, and scripts, but I can't get it to POST my group. Has anyone done this? Or can you see what my xml is missing? I've also tried to post using id, and that also doesn't seem to work.

Thanks!

-tep

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor II

Hi @tep Your xml file is malformed. The </computers> tag near the end actually needs to be written as <computers/> because you aren't specifying any actual computers in that group, which of course makes sense since you want it to populate from the JSS inventory information.

BTW, a good way to check your xml for format errors is to use xmllint. There are some other commands as well, but that's the one I use.

xmllint --format /path/to/NoCheckin.xml

If it gives you an error such as:

parser error : Opening and ending tag mismatch: computer_group line 1 and computers
han x days ago</search_type><value>50</value></criterion></criteria></computers>
                                                                               ^

Then there is a problem. Good news is, the ^ actually points to where the problem is in the tags. In this case it indicated that </computers> was the problem tag.

Once I changed the tag as I mentioned above and re-ran xmllint against it, produced:

<?xml version="1.0" encoding="UTF-8"?>
<computer_group>
  <name>No check-in</name>
  <is_smart>true</is_smart>
  <criteria>
    <size>1</size>
    <criterion>
      <name>Last Check-in</name>
      <priority>0</priority>
      <and_or>and</and_or>
      <search_type>more than x days ago</search_type>
      <value>50</value>
    </criterion>
  </criteria>
  <computers/>
</computer_group>

which looks much better πŸ™‚

Hope that helps.

View solution in original post

5 REPLIES 5

mm2270
Legendary Contributor II

Hi @tep Your xml file is malformed. The </computers> tag near the end actually needs to be written as <computers/> because you aren't specifying any actual computers in that group, which of course makes sense since you want it to populate from the JSS inventory information.

BTW, a good way to check your xml for format errors is to use xmllint. There are some other commands as well, but that's the one I use.

xmllint --format /path/to/NoCheckin.xml

If it gives you an error such as:

parser error : Opening and ending tag mismatch: computer_group line 1 and computers
han x days ago</search_type><value>50</value></criterion></criteria></computers>
                                                                               ^

Then there is a problem. Good news is, the ^ actually points to where the problem is in the tags. In this case it indicated that </computers> was the problem tag.

Once I changed the tag as I mentioned above and re-ran xmllint against it, produced:

<?xml version="1.0" encoding="UTF-8"?>
<computer_group>
  <name>No check-in</name>
  <is_smart>true</is_smart>
  <criteria>
    <size>1</size>
    <criterion>
      <name>Last Check-in</name>
      <priority>0</priority>
      <and_or>and</and_or>
      <search_type>more than x days ago</search_type>
      <value>50</value>
    </criterion>
  </criteria>
  <computers/>
</computer_group>

which looks much better πŸ™‚

Hope that helps.

View solution in original post

jhbush
Valued Contributor II

@mm2270 worked for me. Nice practical example of API usage.

tep
Contributor II

@mm2270 Worked like a charm. Thanks - you just saved me a ton of work!

tep
Contributor II

Interesting development: after updating to 9.72, my import no longer works. SAME xml file! I deleted the group and tried to re-add it, using the exact same method as before, and no import. Looking at the API "documentation" (myjss:8443/api) nothing has changed. Strange.

ChrisL
New Contributor III

If you're comfortable in ruby at all, just install the jss-api ruby gem (http://pixaranimationstudios.github.io/jss-api-gem/index.html)

then these few lines will do it, no xml needed

#!/usr/bin/ruby

# load in the library
require 'jss-api'

# connect to the JSS
JSS::API.connect :server => 'casper.mysite.org', :user => 'myjssuser',  :pw => :prompt, :verify_cert => false

# make a new, uncreated smart computer group
new_smartgroup = JSS::ComputerGroup.new :id => :new, :type => :smart, :name => "no-recent-checkin"

# create a criterion for group membership
criterion = JSS::Criteriable::Criterion.new :and_or => :and, :name => "Last Check-in", :search_type => "more than x days ago", :value => 50

# add the criterion to the group
new_smartgroup.criteria = JSS::Criteriable::Criteria.new [criterion]

# create the group in the JSS
new_smartgroup.create

# the membership is now available in new_smartgroup.members