Issue when trying to copy a policy from one JSS to another

ajacks88
New Contributor

Hello, I am attempting to write a script that will run a GET method against one JSS (to obtain a policy), and then run a POST against another JSS (to essentially clone that policy to the second JSS.)

The problem has to do with the Category. The category of the policy in the first JSS does not exist in the second. Thus i need to edit the XML between the GET and POST to reflect the new category. However, when i run the POST, i get the following error:

Conflict
Error: Problem with category
You can get technical details here.
Please continue your visit at our home page.

I've tried: not editing the category, changing the category name and ID to a known good category in the second JSS, as well as just removing the category name and ID all together. No luck.

I am hoping someone here has tried to do something similar to this, and maybe has some advice to give about what I am missing with the category.

I am most comfortable with PowerShell, i tried to comment my code so you can tell what i am doing.

# Declare the old and new JSS urls
$OldJSSURL = 'https://oldjss.com:8443'
$NewJSSURL = 'https://newjss.com:8443'

# Create a variable with my credentials in it
$Creds = Get-Credential

# Add the web type so i can encode text strings for rest methods
Add-Type -AssemblyName System.Web

# Declare the policy name i would like to migrate
$OldPolicyName = 'Test Policy'

# Encode the policy name so it will work when using a rest method
$OldPolicyEncodedName = [System.Web.HTTPUtility]::UrlEncode($OldPolicyName)

# Declare the URI to use when getting the old policy from the old JSS
$GetOldPolicyURI = $OldJSSURL + "/JSSResource/policies/name/$OldPolicyEncodedName"

# Get the old policy
$GetOldPolicy = Invoke-RestMethod -Method GET -URI $GetOldPolicyURI -Credential $Creds

# Declare the URI for creating the new policy in the new JSS
$CreateNewPolicyURI = $NewJSSURL + "/JSSResource/policies/id/0"

# Create a variable with just the XML in it
$PolicyXML = $GetOldPolicy.policy

# Declare the new category name
$NewCategoryName = 'Unknown'

# Declare the new category id
$NewCategoryID = '1'

# Set the XML to reflect the new category id and name
$PolicyXML.general.category.id = $NewCategoryID
$PolicyXML.general.category.name = $NewCategoryName

# Declare the headers and body
$Headers = @{}
$Headers["Accept"] = "application/xml"
$Body = $PolicyXML

# Create the new policy in the new jss
$CreateNewPolicy = Invoke-RestMethod -Method Post -Uri $CreateNewPolicyURI -Headers $Headers -Body $Body -Credential $creds -ContentType "application/xml"
7 REPLIES 7

BradB
New Contributor III

Try to add an additional header with a key of "Content-Type" and a value of "application/xml". I believe the "Accept" header is setting the type of content you want returned to you instead of indicating the type of content you are attempting to POST.

ajacks88
New Contributor

Hey @beckerbm, thanks for the reply. PowerShell's Invoke-RestMethod cmdlet actually has a built in switch for ContentType (i had it at the end of the last line in the script.) However, i tried removing that and adding it to the headers, but i am still getting the same error back.

BradB
New Contributor III

Ah, I didn't scroll over far enough to see that you included the ContentType switch in your command. Have you tried to perform the POST with only the category ID value and not the category name? It's strange that it doesn't work even when you remove the category tags all together.

ajacks88
New Contributor

Very strange, indeed.

Just to make sure i wasn't doing something fundamentally wrong in my syntax i hot-swapped the $Body variable for a $TestBody variable that was set to: '<policy><general><name>test1</name></general></policy>'. This command went through just fine, and the new test policy was created using the rest of my script. But for some reason when i try to migrate a policy from one JSS to another, it doesn't seem to think the Category is OK.

Let me try to spot-check a few other policies, and try to migrate them using this script, just to rule out the chance that this one policy I've been testing with is somehow broken.

ajacks88
New Contributor

Apologies, i forgot to mention that i tried to send just the category ID and not the name, but received the same error.

ajacks88
New Contributor

Strange, on the Test Policy i created, i gave the command no category information in the body, and when it was created in the JSS it was given a category ID of -1 and a name of No Category Assigned. When i try to use -1 and no category assigned in the command it gives the same error.

I've been trying to tweak the category xml to see if i can figure out why it isn't happy. When i put in just: <category><id></id></category>, the error is Error: No match found for category, but as soon as i add in an integer it goes back to the same error message.

leslie
Contributor II
Contributor II

Try not setting/using a category ID when creating the XML for the post. Just specify the category name if desired. Note the category needs to already exist in the new server.