powershell - 将 txt 数据与变量进行比较 - 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
此外,我尝试使用不带逗号的输入分隔行,脚本无法与 $baseline 变量进行比较。
期望的结果是:将补丁编号写入文本文件,将新基线附加到 vmware 环境,并将主机上安装的补丁与我写入文本的补丁进行比较。
非常感谢您的帮助!
解决方案
该-IncludePatch
参数需要一个项目数组。首先,我建议删除-Raw
开关,Get-Content
因为它会将文件内容作为一个长字符串读入。其次,我建议一次只列出一行补丁。该组合将导致文件被读取为字符串数组,每个字符串都是补丁名称。
# patches.txt Contents
ESXi670-201912001
ESXi670-201905001
# Update This Line
$patches = Get-Content $path\patches.txt
推荐阅读
- reactjs - 如何在单击按钮时更改字体大小?
- android - 保存到 SD 卡时无法离线播放下载的 Amazon Prime 视频
- reactjs - 你如何在 React JS 中重置加载栏?
- javascript - Javascript 添加多个 if - window.location.hostname
- excel - 从许多来源批量复制 Excel 工作表
- android - 尝试显示位图时应用程序崩溃
- git - Gitlab 可以用作无头 CMS 吗?
- file - 异步任务可以写入内部存储android吗?
- xml - 选择包含额外属性的特定元素
- javascript - 成员的类继承