0

我正在尝试将基于 WMI 的脚本转换为 CIM,该脚本能够将 IP 地址注入 Hyper-V Virtual Mahine 原始脚本是: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 语句修改这些只读属性?

提前致谢。

4

2 回答 2

1

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

于 2016-11-07T16:07:01.827 回答
0

Set-CimInstance不适用于只读属性。你需要打电话Invoke-CimMethod。有关示例用法,请参阅使用 PowerShell和SetTcpipNetbios调用 CIM 方法(复制以供参考)。

# 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 '}
    }
}
于 2021-01-02T15:13:11.077 回答