比较文本数据与变量 - powershell

Compare txt data with variable - powershell

这是我要使用的脚本:

$path = split-path $MyInvocation.MyCommand.path

$vcenter = Read-Host "Please enter the vCenter name where You want to connect"

Import-Module -Name VMware.VimAutomation.Core
Connect-VIserver $vcenter

$folderName = 'Datacenters'
$folder = Get-Folder -Name $folderName
$patches = Get-Content $path\patches.txt -Raw
$baseline = New-PatchBaseline -Name "Baseline$(Get-Random)" -Static -IncludePatch $patches

Attach-Baseline -Entity $folder -Baseline $baseline -Confirm:$false

Scan-Inventory -Entity $folder

Get-Compliance -baseline $baseline -entity $folder | select Entity, Status

Detach-Baseline -Entity $folder -Baseline $baseline -Confirm:$false

Remove-Baseline -Baseline $baseline -Confirm:$false

如果我将多个补丁号写入 txt - 我尝试了以下方法 - :

ESXi670-201912001,ESXi670-201905001

ESXi670-201905001ESXi670-201912001

我还尝试用 enter 分隔行,没有逗号,脚本无法与 $baseline 变量进行比较。

期望的结果是:将补丁编号写入文本文件,将新基线附加到 vmware 环境,并将主机上安装的补丁与我写入文本的补丁进行比较。

非常感谢您的帮助!

-IncludePatch 参数需要一个项目数组。首先,我建议从 Get-Content 中删除 -Raw 开关,因为这会将文件内容作为一个长字符串读入。其次,我建议一次只列出一行补丁。该组合将导致文件被读取为字符串数组,每个字符串都是一个补丁名称。

# patches.txt Contents
ESXi670-201912001
ESXi670-201905001

# Update This Line
$patches = Get-Content $path\patches.txt