无法使用System.Management API或Power Shell从WMI类更新读/写属性 [英] Cannot update a Read/Write property from WMI class by using System.Management API or Power Shell

查看:53
本文介绍了无法使用System.Management API或Power Shell从WMI类更新读/写属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好, 
$


我尝试从MDM_Policy_Result01_Connectivity02类更新属性,名为

AllowBluetooth根据文档应该是读取/写入,但是当我这样做时,我收到了ReadOnly属性错误。


这些新的WMI类在Windows 10上可用,并提供给了
桥接设备的DM设置:



https://msdn.microsoft.com/en-us/library/dn905224(v=vs.85 ).aspx



$
首先,我试图通过使用来自
a的System.Management命名空间来更新值常规桌面应用程序通过以下例程:



$
            ManagementScope范围=新的ManagementScope("ROOT \\CIMV2 \\mdm \\dmmap");

                String strWQL =" SELECT * FROM MDM_Policy_Result01_Connectivity02";

                WqlObjectQuery query = new WqlObjectQuery(strWQL);

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query,null);




                if(searcher.Get()。Count> 0)

                {

                    foreach(searcher.Get()中的ManagementObject实例)

                    {

                        foreach(PropertyData prop in instance.Properties)

                        {

                            MessageBox.Show(prop.Name +" =" + prop.Value);

                            if(prop.Name ==" AllowBluetooth")

                            {

                                prop.Value = 0;

                                PutOptions opt = new PutOptions();

                                opt.Type = PutType.UpdateOnly;

                                instance.Put(opt);

                                MessageBox.Show(" Property updated successfully !!");

                            }¥b $ b                        }¥b $ b                    }¥b $ b                }






我还尝试使用PowerShell脚本:





$ mdmClass = Get-WmiObject -namespace root\CIMV2 \mdm \dmmap-Class MDM_Policy_Result01_Connectivity02 -Filter" InstanceID ='Connectivity'AND ParentID = './Vendor/MSFT/Policy/Result'"
$


写主机"在更新前允许蓝牙=" $ mdmClass.AllowBluetooth



$ putOpt = New-Object System.Management.PutOptions



$ putOpt。 Type = [System.Management.PutType] :: UpdateOnly



$ mdmClass.AllowBluetooth = 0



#$ mdmClass.Put()不起作用



$ mdmClass.Put($ putOpt)



写主机"AllowBluetooth =更新后" $ mdmClass.AllowBluetooth



$
对于这两种情况我无法更新属性,但PowerShell输出错误显示更详细的信息

有关正在发生的信息:



" AllowBluetooth"是一个ReadOnly属性。

在C:\ Users \oliveira.SIDI \Documents\scripts\Untitled1.ps1:16 char:1

+ $ mdmClass.AllowBluetooth = 0

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         :NotSpecified:(:) [],SetValueException

    + FullyQualifiedErrorId:ReadOnlyWMIProperty



异常调用" Put"用"0"表示"0"。参数:"对象或属性已存在"
$
在C:\ Users \oliveira.SIDI \ Documents \ _scripts \ Untitled1.ps1:20 char:1 < br $>
+ $ mdmClass.Put()

+ ~~~~~~~~~~~~~~~~
  &NBSP; + CategoryInfo  &NBSP; &NBSP; &NBSP;  :NotSpecified:(:) [],MethodInvocationException

  &NBSP; + FullyQualifiedErrorId:DotNetMethodException



$


您是否知道为什么不能更改此属性? ? 
$


$
您拥有的任何信息都会很棒!

Hi everyone, 

I trying to update a property from MDM_Policy_Result01_Connectivity02 class called
AllowBluetooth that according documentation it's supposed to be read/write but I'm getting ReadOnly property error when I do it

These new WMI classes are available on Windows 10 and are provided to be the
bridge with DM settings from device:

https://msdn.microsoft.com/en-us/library/dn905224(v=vs.85).aspx


Firstly, I tried to update the value by using System.Management namespace from
a regular Desktop application through the routine below:


            ManagementScope scope = new ManagementScope("ROOT\\CIMV2\\mdm\\dmmap");
                String strWQL = "SELECT * FROM MDM_Policy_Result01_Connectivity02";
                WqlObjectQuery query = new WqlObjectQuery(strWQL);
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, null);


                if (searcher.Get().Count > 0)
                {
                    foreach (ManagementObject instance in searcher.Get())
                    {
                        foreach (PropertyData prop in instance.Properties)
                        {
                            MessageBox.Show(prop.Name + " = " + prop.Value);
                            if (prop.Name == "AllowBluetooth")
                            {
                                prop.Value = 0;
                                PutOptions opt = new PutOptions();
                                opt.Type = PutType.UpdateOnly;
                                instance.Put(opt);
                                MessageBox.Show("Property updated successfully!!");
                            }
                        }
                    }
                }



And I also tried by using a PowerShell script:


$mdmClass = Get-WmiObject -namespace root\CIMV2\mdm\dmmap -Class MDM_Policy_Result01_Connectivity02 -Filter "InstanceID='Connectivity' AND ParentID='./Vendor/MSFT/Policy/Result'"

Write-Host "AllowBluetooth before update = " $mdmClass.AllowBluetooth

$putOpt = New-Object System.Management.PutOptions

$putOpt.Type = [System.Management.PutType]::UpdateOnly

$mdmClass.AllowBluetooth = 0

#$mdmClass.Put() does not work as well

$mdmClass.Put($putOpt)

Write-Host "AllowBluetooth = after update" $mdmClass.AllowBluetooth


For both cases I could not update the property, but PowerShell output error showed me more detailed
information about was happening:

"AllowBluetooth" is a ReadOnly property.
At C:\Users\oliveira.SIDI\Documents\scripts\Untitled1.ps1:16 char:1
+ $mdmClass.AllowBluetooth = 0
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueException
    + FullyQualifiedErrorId : ReadOnlyWMIProperty

Exception calling "Put" with "0" argument(s): "Object or property already exists "
At C:\Users\oliveira.SIDI\Documents\scripts\Untitled1.ps1:20 char:1
+ $mdmClass.Put()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException



Do you have any idea about why it's not possible to change this property? 


Any information you have would be great!

推荐答案

根据文档, MDM_Policy_Result01_Connectivity02 是只读的:

https://msdn.microsoft.com/en-us/library/windows/desktop/dn905253%28v=vs.85% 29.aspx
的。您可能需要
MDM_Policy_Config01_Connectivity02

According to documentation, MDM_Policy_Result01_Connectivity02 is read-only: https://msdn.microsoft.com/en-us/library/windows/desktop/dn905253%28v=vs.85%29.aspx. You probably need MDM_Policy_Config01_Connectivity02.


这篇关于无法使用System.Management API或Power Shell从WMI类更新读/写属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆