简体   繁体   English

是否可以更改设备策略?

[英]Is it possible to change a device policy?

My enterprise has 2 policies: POLICY_1 and POLICY_2.我的企业有 2 个策略:POLICY_1 和 POLICY_2。 My device was provisioned with the POLICY_1, but I would like to change it to POLICY_2.我的设备配置了 POLICY_1,但我想将其更改为 POLICY_2。

I'm using the Java lib 'com.google.apis:google-api-services-androidmanagement:v1-rev20201012-1.30.10'.我正在使用 Java lib 'com.google.apis:google-api-services-androidmanagement:v1-rev20201012-1.30.10'。

I'm trying to change its policy with the following code:我正在尝试使用以下代码更改其策略:

private void updateMyDevicePolicy(String enterpriseName, String deviceName) throws IOException {
    Device device = getMyDevice(deviceName);
    String policyName = enterpriseName + "/policies/POLICY_2";
    device.setPolicyName(policyName);
    device.setAppliedPolicyName(policyName);
    Device deviceUpdated = androidManagementClient.enterprises().devices().patch(deviceName, device).execute();
    
    System.out.println("DEVICE policy updated = " + deviceUpdated);
}

The problem is: The patch command does not work.问题是:patch 命令不起作用。 The device policy remains "POLICY_1".设备策略仍为“POLICY_1”。

Please, how can I change the device policy?请问,如何更改设备策略?

This is very much possible and I do it regularly too.这是很有可能的,我也经常这样做。 Use the Device Patch request and pass in the PolicyId that needs to be applied to the device.使用Device Patch请求并传入需要应用到设备的 PolicyId。

The name of the policy applied to the device, in the form enterprises/{enterpriseId}/policies/{policyId}.应用于设备的策略名称,格式为 enterprise/{enterpriseId}/policies/{policyId}。 This field can be modified by a patch request.该字段可以通过补丁请求进行修改。 You can specify only the policyId when calling enterprises.devices.patch, as long as the policyId doesn't contain any slashes.您可以在调用 enterprise.devices.patch 时仅指定 policyId,只要 policyId 不包含任何斜线即可。 The rest of the policy name is inferred.推断策略名称的 rest。 Ref 参考

Here's a overview of my C# code.这是我的 C# 代码的概述。

  1. Create the Device Request body with the policy that needs to be applied to it.使用需要应用的策略创建设备请求正文。
Google.Apis.AndroidManagement.v1.Data.Device deviceBody = new Device()
{
    Name = device.DeviceId,
    PolicyName = POLICY_2,
    State = "ACTIVE"
};
  1. Invoke the Device Patch request调用Device Patch请求
EnterprisesResource.DevicesResource.PatchRequest attachNewPolicy = 
new EnterprisesResource.DevicesResource.PatchRequest(service, deviceBody, device.DeviceId);
attachNewPolicy.Execute();

And this should apply the new policy to the device.这应该将新策略应用于设备。

Updating the policy of a device can be done using devices().patch().可以使用 devices().patch() 更新设备的策略。 However, it is necessary to set the updateMask in devices().patch() to which field will be updated, which in this case is the policyName, to tell that you only want to change the that certain field.但是,有必要在 devices().patch() 中设置 updateMask 将更新哪个字段,在本例中为 policyName,以告知您只想更改该特定字段。

Below is the implementation of updateMask using different language:以下是使用不同语言的 updateMask 实现:

androidmanagement.enterprises().devices().patch(
    name=device_name,
     updateMask="policyName",
    body=json.loads(devices_json)
).execute()

In this code, the “policyName” was updated from enterpriseName + "/policies/POLICY_1" to enterpriseName + "/policies/POLICY_2"在此代码中,“policyName”从 enterpriseName + "/policies/POLICY_1" 更新为 enterpriseName + "/policies/POLICY_2"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM