You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell操作VMware补丁基线报错:无法转换对象数组至Patch类型

解决PowerShell中VMware补丁基线的类型转换错误

问题场景

通过PowerShell自动化获取最新VMware Tools更新,将其添加到新的补丁基线;若基线已存在,则把更新纳入该基线。执行脚本时触发如下报错:

Cannot convert 'System.Object[]' to the type 'VMware.VumAutomation.Types.Patch' required by parameter 'IncludePatch'

原代码片段:

$baseline = Get-PatchBaseline -Name $baseline_name
# 获取最新版本的VMware Tools补丁
$latest_tools = Get-Patch | Where-Object {$_.Name -like "*VMware Tools*"}| Sort-Object -Property Version | Select-Object -Last 1
$patches = Get-Patch -Server $server -Before $before_date

# 出错的代码行:
$all_patches = $patches, $latest_tools

# 基线不存在则新建,否则更新
if (-not $baseline){
    Download-Patch
    $baseline = New-PatchBaseline -Server $server -Name $baseline_name -Static -IncludePatch $all_patches
}
else {
    Set-PatchBaseline -Baseline $baseline -IncludePatch $all_patches
}

已尝试的写法(均未解决):

$all_patches = @($patches, $latest_tools)
$patches = $patches, $latest_tools
$patches += $latest_tools
$patches.Add($latest_tools)

问题原因

核心问题是数组嵌套:

  • 当$patches是多个Patch对象组成的数组时,$patches, $latest_tools会生成一个二维数组(数组的数组),而IncludePatch参数需要的是单个Patch对象的扁平数组。
  • 控制台直接运行时,$patches可能恰好是单个对象,不会触发嵌套;但脚本执行时$patches通常是多对象数组,拼接后就会出现类型转换错误。

解决方案

以下几种写法可生成符合要求的扁平Patch对象数组:

方法1:确保基础数组类型后追加

先通过@()强制$patches为数组,再用+=追加补丁:

$baseline = Get-PatchBaseline -Name $baseline_name
$latest_tools = Get-Patch | Where-Object {$_.Name -like "*VMware Tools*"}| Sort-Object Version | Select-Object -Last 1
# 强制转为数组,避免单个对象时的类型问题
$patches = @(Get-Patch -Server $server -Before $before_date)
# 追加最新工具补丁
$patches += $latest_tools

if (-not $baseline){
    Download-Patch
    $baseline = New-PatchBaseline -Server $server -Name $baseline_name -Static -IncludePatch $patches
}
else {
    Set-PatchBaseline -Baseline $baseline -IncludePatch $patches
}

方法2:用分号展开拼接数组

使用@()结合分号,直接生成扁平数组:

$all_patches = @($patches; $latest_tools)

方法3:合并查询(高效写法)

直接在获取补丁时合并结果,避免后续拼接:

$latest_tools = Get-Patch | Where-Object {$_.Name -like "*VMware Tools*"}| Sort-Object Version | Select-Object -Last 1
# 获取指定日期前的补丁,排除已包含的最新工具补丁,再合并
$all_patches = @(
    Get-Patch -Server $server -Before $before_date | Where-Object { $_ -ne $latest_tools }
    $latest_tools
)

额外注意事项

  • 始终用@()包裹Get-Patch的返回结果,确保结果是数组类型,即使只有一个对象。
  • 验证$latest_tools是否为单个Patch对象:可通过$latest_tools.GetType().Name检查,若返回Object[],需调整Where-Object的过滤条件,确保只匹配一个补丁。

内容的提问来源于stack exchange,提问作者Progs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 01:15:04