简体   繁体   English

通过PowerCLI和ReconfigVM从VM Advanced Config中删除条目

[英]Remove an entry from VM Advanced Config via PowerCLI and ReconfigVM

http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html#reconfigure http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html#reconfigure

I'm writing a script to update the VMX state for virtual machines to comply with some standards, but also want to backup their state in case they need to re restored. 我正在编写脚本来更新虚拟机的VMX状态,以使其符合某些标准,但是我还想备份其状态,以防它们需要恢复。 The ReconfigVM task works fine, and I can pull the state via ($VM | Get-View).config.extraconfig ReconfigVM任务运行正常,我可以通过($VM | Get-View).config.extraconfig拉状态

What I can't figure out is how to remove items using VirtualMachineConfigSpec . 我不知道如何使用VirtualMachineConfigSpec删除项目。

Let's say I update a value as such. 假设我这样更新一个值。

$ExtraOptions = @{
    "isolation.device.connectable.disable"="true";
    "log.rotateSize"="100000";
}

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
Foreach ($Option in $ExtraOptions.GetEnumerator()) {
    $OptionValue = New-Object VMware.Vim.optionvalue
    $OptionValue.Key = $Option.Key
    $OptionValue.Value = $Option.Value
    $vmConfigSpec.extraconfig += $OptionValue
}
# ...

foreach($vm in $vms){
    $vm.ReconfigVM($vmConfigSpec)
}

Now let's say I create a backup hashtable of the config spec for the values prior to changing them. 现在,假设我在更改值之前为配置规范创建了一个备份哈希表。 Since these values did not exist prior to the update there's no value prior. 由于这些值在更新之前不存在,因此没有优先值。

A $null throws an error. $null引发错误。

"isolation.device.connectable.disable"=$null;

An empty "" does no update 空的""不更新

"isolation.device.connectable.disable"="";

I could simply revert the Boolean values and set integer values to 0 , but I'd rather just remove the value from the configuration. 我可以简单地还原布尔值并将整数值设置为0 ,但是我宁愿只是从配置中删除该值。

The documentation states the following about extraconfig 文档指出以下有关extraconfig

Additional configuration information for the virtual machine. 虚拟机的其他配置信息。 This describes a set of modifications to the additional options. 这描述了对其他选项的一组修改。 An option is removed if the key is present but the value is not set or the value is an empty string. 如果键存在但未设置值或值为空字符串,则删除选项。 Otherwise, the key is set to the new value. 否则,将密钥设置为新值。

Configuration keys that would conflict with parameters that are explicitly configurable through other fields in the ConfigSpec object are silently ignored. 与将通过ConfigSpec对象中的其他字段显式配置的参数冲突的配置键将被忽略。

EDIT 编辑

Passing a whitespace character (ie " " ) will set the value to false or 0 . 传递一个空格字符(即" " )会将值设置为false0

显然,您必须下载vmx文件(存储这些选项的位置),然后在关闭虚拟机电源的情况下重新上传修改后的文件,并且可能也未注册。

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig = New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key="cloud.uuid"
$vmConfigSpec.extraconfig[0].Value=""
$vm.ReconfigVM($vmConfigSpec)

Taking the plus sign out along with value of nothing "" should remove the line from the vmx file. 加上加号(不带任何值)将从vmx文件中删除该行。

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

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