Search code examples
powershellvmwarevcenter

Compare txt data with variable - powershell


This is the script what I want to use:

$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

If I write multiple patch numbers into the txt - I tried the following methods - :

ESXi670-201912001,ESXi670-201905001

ESXi670-201905001ESXi670-201912001

Also I tried to separate the lines with enter, without comma, the script not able to compare with the $baseline variable.

The desired result would be: write the patch numbers into the text file, attach a new baseline to the vmware environment, and compare the installed patches on the hosts, with the patches what I wrote into the text.

Many thanks for the help!


Solution

  • The -IncludePatch parameter expects an array of items. First, I recommend removing the -Raw switch from Get-Content because that will read in the file contents as one, long string. Second, I recommend just listing the patches one line at a time. That combination will cause the file to be read as an array of strings with each string being a patch name.

    # patches.txt Contents
    ESXi670-201912001
    ESXi670-201905001
    
    # Update This Line
    $patches = Get-Content $path\patches.txt