-
Notifications
You must be signed in to change notification settings - Fork 3
/
vpp.gs
281 lines (225 loc) · 7.24 KB
/
vpp.gs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
function assignStandardAppsToDevice(serial) {
var apps = [
{"pricingParam": "STDQ", "adamIdStr": "409183694", "name": "Keynote", "deviceFamilies": ["mac"]},
{"pricingParam": "STDQ", "adamIdStr": "409201541", "name": "Pages", "deviceFamilies": ["mac"]},
{"pricingParam": "STDQ", "adamIdStr": "682658836", "name": "GarageBand", "deviceFamilies": ["mac"]},
{"pricingParam": "STDQ", "adamIdStr": "1244188551", "name": "Carousel Player", "deviceFamilies": ["tvos"]},
{"pricingParam": "STDQ", "adamIdStr": "409203825", "name": "Numbers", "deviceFamilies": ["mac"]},
{"pricingParam": "STDQ", "adamIdStr": "408981434", "name": "iMovie", "deviceFamilies": ["mac"]}
];
var device = getDEPDevices([serial])[serial];
var family = device.device_family;
if ( family == "Mac" ) { family = "mac" };
if ( family == "AppleTV" ) { family = "tvos" };
var responses = [];
for ( var i=0; i<apps.length; i++ ) {
var deviceFamilies = apps[i].deviceFamilies;
for ( var j=0; j<deviceFamilies.length; j++ ) {
if ( family == deviceFamilies[j] ) {
responses.push(manageVPPLicensesByAdamIdSrv(getToken(), Number(apps[i].adamIdStr), apps[i].pricingParam, [serial]));
}
}
}
return responses;
}
function testInstall() {
Logger.log(installAllVppAppsForDevice("C02JDFA1DTY3","3AAD67F9-BDE5-5B4B-9B88-2B47551D1E0D"));
}
function installAllVppAppsForDevice(serial, udid) {
if ( udid ) {
var licenses = getLicencesForDevice(serial);
for ( var i=0; i<licenses.length; i++ ) {
var id = Number(licenses[i].adamIdStr);
installVPPApplication(udid, id);
}
}
}
function getVPPServiceConfigSrv() {
var properties = PropertiesService.getScriptProperties();
return JSON.parse(properties.getProperty('VPPServiceConfigSrv'));
}
function setVPPServiceConfigSrv(data) {
var properties = PropertiesService.getScriptProperties();;
properties.setProperty('VPPServiceConfigSrv', data);
}
function syncVPPServiceConfigSrv() {
var response = VPPServiceConfigSrv();
setVPPServiceConfigSrv(response);
}
function getToken() {
var files = DriveApp.getFilesByName(VPP_TOKEN);
var file = files.next();
var token = file.getBlob().getDataAsString();
return token;
}
function VPPServiceConfigSrv() {
var url = 'https://vpp.itunes.apple.com/WebObjects/MZFinance.woa/wa/VPPServiceConfigSrv';
var options = {
"method" : "GET",
"contentType" : "application/json"
};
var response = UrlFetchApp.fetch(url, options);
return response;
}
function getLicencesForDevice(serial) {
return getVPPLicensesSrv(getToken(), serial).licenses;
}
function getVPPLicensesSrv(sToken, serial, id, batch, modified, pricing, facilitator, assigned, device, user) {
var data = {
"sToken": sToken
};
if ( serial ) { data["serialNumber"] = serial; }
if ( batch ) { data["batchToken"] = batch; }
if ( modified ) { data["sinceModifiedToken"] = modified; }
if ( facilitator ) { data["facilitatorMemberId"] = facilitator; }
if ( assigned == true ) { data["assignedOnly"] = assigned; }
if ( device == true ) { data["deviceAssignedOnly"] = device; }
if ( !device && (user == true) ) { data["userAssignedOnly"] = user; }
if ( !batch && !modified ) {
if ( id ) {
data["adamId"] = id;
if ( pricing ) {
data["pricingParam"] = pricing;
}
}
}
var url = getVPPServiceConfigSrv().getLicensesSrvUrl;
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response);
}
function getVPPAssetsSrv(sToken, licenses, pricing, facilitator) {
var data = {
"sToken": sToken
};
if ( licenses == true ) {
data["includeLicenseCounts"] = true;
}
if ( pricing ) {
data["pricingParam"] = pricing;
}
if ( facilitator ) {
data["facilitatorMemberId"] = facilitator;
}
var url = getVPPServiceConfigSrv().getVPPAssetsSrvUrl;
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response);
}
function setClientContext() {
var data = {"hostname":getServerUrl(),"guid":Utilities.getUuid()};
Logger.log(VPPClientConfigSrv(getToken(), data));
}
function VPPClientConfigSrv(sToken, clientContext, verbose) {
var data = {
"sToken": sToken
};
if ( clientContext ) {
data["clientContext"] = JSON.stringify(clientContext);
}
data["verbose"] = "false";
var url = getVPPServiceConfigSrv().clientConfigSrvUrl;
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
return response;
}
function manageVPPLicensesByAdamIdSrv(sToken, id, pricing, assign, unassign, notify, facilitator) {
var data = {
"sToken": sToken,
"adamIdStr": id,
"pricingParam": pricing
};
if ( assign ) {
if ( assign[0].length == 12 ) {
data["associateSerialNumbers"] = assign;
} else {
data["associateClientUserIdStrs"] = assign;
}
}
if ( unassign ) {
if ( unassign[0].length == 12 ) {
data["disassociateSerialNumbers"] = unassign;
} else if ( unassign[0].length < 12 ) {
data["disassociateLicenseIdStrs"] = unassign;
} else {
data["disassociateClientUserIdStrs"] = unassign;
}
}
if ( notify == false ) {
data["notifyDisassociation"] = notify;
}
if ( facilitator ) {
data["facilitatorMemberId"] = facilitator;
}
var url = getVPPServiceConfigSrv().manageVPPLicensesByAdamIdSrvUrl;
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
return response;
}
function getVppAppsForToken(sToken) {
var response = getVPPAssetsSrv(sToken);
var assets = response.assets;
var apps = [];
for ( var i=0; i<assets.length; i++ ) {
var id = assets[i].adamIdStr;
var pricing = assets[i].pricingParam;
var metadata = getBasicAppData(id);
if ( metadata ) {
var name = metadata.name;
var families = metadata.deviceFamilies;
var data = {
"adamIdStr": id,
"name": name,
"deviceFamilies": families,
"pricingParam": pricing
};
apps.push(data);
}
}
return apps;
}
function getBasicAppData(id, version) {
if ( version ) {
var response = metadataLookup(id, version);
} else {
var response = metadataLookup(id);
}
var results = response["results"];
for ( var result in results ) {
var data = results[result];
var name = data.name;
var families = data.deviceFamilies;
return {"name": name, "deviceFamilies": families};
}
}
function metadataLookup(id, version, platform) {
var url = getVPPServiceConfigSrv().contentMetadataLookupUrl + '?p=mdm-lockup&caller=MDM&cc=us&l=en&id=' + id;
if ( version ) {
url += '&version=' + version;
}
if ( platform ) {
url += '&platform=' + platform;
}
var options = {
"method" : "GET",
"contentType" : "application/json"
};
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response);
}