【问题标题】:Powershell WMI vs CIM same object returns different properties attributesPowershell WMI 与 CIM 相同的对象返回不同的属性属性
【发布时间】:2016-11-07 15:57:02
【问题描述】:

我正在尝试将基于 WMI 的脚本转换为 CIM,此脚本能够将 IP 地址注入 Hyper-V 虚拟机 原文为:http://www.ravichaganti.com/blog/?p=2766

就我而言,我已将 WMI 转换为 CIM 语句,如下所示:

$vmname="mytestvm"
        $vm=get-ciminstance -namespace 'root\virtualization\v2' -Class 'Msvm_ComputerSystem' -ComputerName $ComputerName | Where-Object { $_.ElementName -eq $vmname } 
          $VMSettings = get-cimassociatedinstance $vm -resultclassname 'Msvm_VirtualSystemSettingData' | Where-Object { $_.VirtualSystemType
    -eq 'Microsoft:Hyper-V:System:Realized' }   
          $vmnetadapters=get-cimassociatedinstance $vmSettings -resultclassname 'Msvm_SyntheticEthernetPortSettingData'

          $NetworkSettings = @( Get-CimAssociatedInstance $vmnetadapters -resultclassname 'Msvm_GuestNetworkAdapterConfiguration' )

到目前为止,一切正常,数据被访问并且我能够看到界面特征。 但是当我尝试像原始脚本一样设置一个值时,我无法修改它,它告诉我属性设置为只读。

这些分配不起作用。

  $NetworkSettings[0].DHCPEnabled = $false
  $NetworkSettings[0].IPAddresses = $IPAddress
  $NetworkSettings[0].Subnets = $Subnet

当我用“Get-Member”检查对象时,我可以看到这些属性只有“get”方法,而“set”方法不可用。

Name             MemberType Definition
----             ---------- ----------
DefaultGateways  Property   string[] DefaultGateways {get;}
DHCPEnabled      Property   bool DHCPEnabled {get;}
DNSServers       Property   string[] DNSServers {get;}
InstanceID       Property   string InstanceID {get;}
IPAddresses      Property   string[] IPAddresses {get;}
IPAddressOrigins Property   uint16[] IPAddressOrigins {get;}
ProtocolIFType   Property   uint16 ProtocolIFType {get;}
PSComputerName   Property   string PSComputerName {get;}
Subnets          Property   string[] Subnets {get;}

使用 WMI 的原始脚本能够修改这些值,但在我使用 CIM 时无法修改

按照微软的WMI和CIM应该是等价的,但是好像有一些区别。

如何使用 CIM 语句修改这些只读属性?

提前致谢。

【问题讨论】:

    标签: powershell hyper-v psobject get-wmiobject


    【解决方案1】:

    如果您正在使用 CIM cmdlet 并想要进行更改,您可以使用 set-ciminstance 来实际进行更改。

    【讨论】:

    • 我已经尝试过了,但没有成功。我得到的 CIM 对象是一个关联实例。我已经看到它具有“CiminstanceProperties”,如果我这样引用: $properties=(Get-CimAssociatedInstance $vmnetadapters -resultclassname 'Msvm_GuestNetworkAdapterConfiguration').CimInstanceProperties $properties["DHCPEnabled"].value=$False
    【解决方案2】:

    Set-CimInstance 不适用于只读属性。 您需要致电Invoke-CimMethod。 用法示例请参考Invoking CIM Methods with PowerShellSetTcpipNetbios(复制以供参考)。

    # define the arguments you want to submit to the method
    # remove values that you do not want to submit
    # make sure you replace values with meaningful content before running the code
    # see section "Parameters" below for a description of each argument.
    $arguments = @{
        TcpipNetbiosOptions = [UInt32](12345)  # replace 12345 with a meaningful value
    }
    
    
    # select the instance(s) for which you want to invoke the method
    # you can use "Get-CimInstance -Query (ADD FILTER CLAUSE HERE!)" to safely play with filter clauses
    # if you want to apply the method to ALL instances, remove "Where...." clause altogether.
    $query = 'Select * From Win32_NetworkAdapterConfiguration Where (ADD FILTER CLAUSE HERE!)'
    Invoke-CimMethod -Query $query -Namespace Root/CIMV2 -MethodName SetTcpipNetbios -Arguments $arguments |
    Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
      switch ([int]$this.ReturnValue)
      {
            0        {'Successful completion, no reboot required'}
            1        {'Successful completion, reboot required'}
            64       {'Method not supported on this platform'}
            65       {'Unknown failure'}
            66       {'Invalid subnet mask'}
            67       {'An error occurred while processing an Instance that was returned'}
            68       {'Invalid input parameter'}
            69       {'More than 5 gateways specified'}
            70       {'Invalid IP  address'}
            71       {'Invalid gateway IP address'}
            72       {'An error occurred while accessing the Registry for the requested information'}
            73       {'Invalid domain name'}
            74       {'Invalid host name'}
            75       {'No primary/secondary WINS server defined'}
            76       {'Invalid file'}
            77       {'Invalid system path'}
            78       {'File copy failed'}
            79       {'Invalid security parameter'}
            80       {'Unable to configure TCP/IP service'}
            81       {'Unable to configure DHCP service'}
            82       {'Unable to renew DHCP lease'}
            83       {'Unable to release DHCP lease'}
            84       {'IP not enabled on adapter'}
            85       {'IPX not enabled on adapter'}
            86       {'Frame/network number bounds error'}
            87       {'Invalid frame type'}
            88       {'Invalid network number'}
            89       {'Duplicate network number'}
            90       {'Parameter out of bounds'}
            91       {'Access denied'}
            92       {'Out of memory'}
            93       {'Already exists'}
            94       {'Path, file or object not found'}
            95       {'Unable to notify service'}
            96       {'Unable to notify DNS service'}
            97       {'Interface not configurable'}
            98       {'Not all DHCP leases could be released/renewed'}
            100      {'DHCP not enabled on adapter'}
            default  {'Unknown Error '}
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2012-04-29
      • 2020-01-29
      • 1970-01-01
      相关资源
      最近更新 更多