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"