-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathExportJamfXML.html
121 lines (114 loc) · 4.44 KB
/
ExportJamfXML.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!doctype html>
<html>
<head>
<meta name=viewport content='initial-scale=1.0, minimum-scale=0.25'>
<title>Export Jamf XML</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Export Jamf XML</h1>
<p>This tool will export Jamf objects to XML.</p>
<b>Setup</b>
<ol>
<li>Show your bookmarks toolbar. In Chrome, … > Bookmarks > Show Bookmarks Bar. In Firefox, right-click in the title bar and click Bookmarks Toolbar.
<li>Drag/drop this <a id=bm>Export Jamf v0.5</a> to the bookmarks toolbar.
</ol>
<br>
<b>Usage</b>
<ol>
<li>Open Jamf and sign in.
<li>Select a Policy or a Smart Computer Group.
<li>Click the "Export Jamf" button from your bookmarks toolbar.
</ol>
<br>
<a href=https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/ExportJamfXML.html>Source code</a>
<script id=code>
(function () {
const paths = location.pathname.split('/');
const folder = paths.length > 2 ? '/' + paths[1] : '';
if (location.pathname.match('/smartComputerGroups.html')) {
var obj = 'computergroup';
var path = 'computergroups';
} else if (location.pathname.match('/policies.html')) {
obj = 'policy';
path = 'policies';
} else {
return;
}
const id = new URLSearchParams(location.search).get('id');
if (!id) {
alert('pick a ' + obj + ' first');
return;
}
const popup = createPopup('rockstar for Jamf');
const form = $('<form><table>' +
'<tr><td style="padding: 8px 8px 8px 0;">Username<td><input id=username style="all: revert; width: 150px;">' +
'<tr><td style="padding: 8px 8px 8px 0;">Password<td><input id=password style="all: revert; width: 150px;" type=password></table><br><br>' +
'<button type=submit>Export</button><br><br><div id=results></div></form>').appendTo(popup);
username.focus();
form.submit(async event => {
event.preventDefault();
var url = folder + '/api/v1/auth/token';
var headers = {
'accept': 'application/json',
'content-type': 'applicaton/json',
'authorization': 'Basic ' + btoa(username.value + ':' + password.value)
};
var response = await fetch(url, {headers, method: 'post'});
if (!response.ok) {
results.innerHTML = 'Invalid username/password.';
return;
}
const apiResponse = await response.json();
const accessToken = apiResponse.token;
popup.html('Loading...');
url = folder + '/JSSResource/' + path + '/id/' + id;
headers = {
'accept': 'application/xml',
'content-type': 'applicaton/xml',
'authorization': 'Bearer ' + accessToken
};
response = await fetch(url, {headers});
const str = await response.text();
const indented = indentXml(str, 4);
const filename = obj + '-' + id + '.xml';
download(indented, 'text/xml', filename);
popup.html('Done.');
});
function indentXml(xml, size) {
const lines = xml.replace(/></g, '>\n<').split('\n');
var level = 0;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var end = line.match('</');
var empty = line.match('/>') || line.match(/>.*</);
if (end && !empty) level--;
lines[i] = ' '.repeat(size * level) + line;
if (!end && !empty) level++;
}
return lines.join('\n');
}
function download(content, type, filename) {
const a = document.body.appendChild(document.createElement('a'));
a.href = URL.createObjectURL(new Blob([content], {type}));
a.download = filename;
a.click();
}
function createPopup(title) {
const popup = $(`<div style='position: absolute; z-index: 1000; top: 0px; max-height: calc(100% - 28px); max-width: calc(100% - 28px); padding: 8px; margin: 4px; overflow: auto; ` +
`background-color: white; border: 1px solid #ddd;'>` +
`${title}<div style='display: block; float: right;'><a href='https://gabrielsroka.github.io/ExportJamfXML.html' target='_blank' rel='noopener' style='padding: 4px'>?</a> ` +
`<a onclick='document.body.removeChild(this.parentNode.parentNode)' style='cursor: pointer; padding: 4px'>X</a></div><br><br></div>`).appendTo(document.body);
return $('<div></div>').appendTo(popup);
}
})
</script>
<script>
bm.href = 'javascript:' + code.innerText + '();';
</script>
</body>
</html>