Uh Trog
Uh Trog

Reputation: 41

Powershell WMI vs CIM same object returns different properties attributes

I'm trying to convert a WMI based script to CIM, this script is able to inject an IP Address to an Hyper-V Virtual Mahine Original Script is : http://www.ravichaganti.com/blog/?p=2766

In my case, I've converted the WMI to CIM sentences like this:

$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' )

Until this point, all works fine, data is accessed and I'm able to see interface characteristics. But when I'll try to set a value like the original script does, I can't modify It, it tells me that property is set as Read Only.

These assignations doesn't work.

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

And when I check the object with "Get-Member" I could see that these properties only have "get" method, and "set" method is not available.

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;}

Original Script, that uses WMI is able to modify these values, but is not possible when I use CIM

According to Microsoft WMI and CIM should be equivalents, but seems that there are some differences.

How can I do it to modify these read-only properties using CIM sentences?

Thanks in advance.

Upvotes: 0

Views: 1069

Answers (2)

Gao
Gao

Reputation: 203

Set-CimInstance doesn't work on read-only properties. You would need to call Invoke-CimMethod. Please refer to Invoking CIM Methods with PowerShell and SetTcpipNetbios (copied for reference below) for example usage.

# 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 '}
    }
}

Upvotes: 0

Mike Garuccio
Mike Garuccio

Reputation: 2718

If you are working with the CIM cmdlets and want to make changes you would use set-ciminstance to actually make the changes.

Upvotes: 1

Related Questions