我需要准备 Powershell 脚本来检查我的 NSG 规则设计表并跳过包含黄色单元格、灰色单元格或空白单元格的行。我已经准备了下面的脚本,但它没有按预期工作。有人可以帮我修复这个脚本吗?
Powershell 脚本:
# Load the EPPlus library
Add-Type -Path "/home/lokesh/EPPlus.dll" # Replace with the path to your EPPlus.dll file
# Create an Excel package object
$excelPackage = New-Object OfficeOpenXml.ExcelPackage
# Load the Excel file
$filePath = "/home/lokesh/NSG_Rules.xlsx"
$excelPackage = [OfficeOpenXml.ExcelPackage]::Load($filePath)
# Get the first worksheet from the Excel package
$worksheet = $excelPackage.Workbook.Worksheets[0] # Change the index if needed
# Define a function to check cell color
function IsYellow($cell) {
return ($cell.Style.Fill.BackgroundColor.Rgb -eq "FFFFFF00") # Yellow color code
}
# Skip rows with yellow, gray, or blank cells
$rowsToKeep = @()
foreach ($row in $worksheet.Cells.Rows) {
$skipRow = $false
foreach ($cell in $row.Cells) {
if (IsYellow($cell) -or $cell.Style.Fill.BackgroundColor.Rgb -eq "FFA9A9A9" -or -not $cell.Value) {
$skipRow = $true
break
}
}
if (-not $skipRow) {
$rowsToKeep += $row
}
}
您问题中的代码在几个基本方面被破坏了;是AI生成的吗?
解决方案如下:
按要求解决了您的问题,但请注意,EPPlus .NET 库对 PowerShell 不是特别友好,而
ImportExcel
PowerShell 模块通常是更好的选择。它:
收集感兴趣的(数据)行作为输出数组中的
[OfficeOpenXml.ExcelRangeRow]
实例以进行进一步处理;每个此类行的单元格都可以通过 .Range
属性进行枚举,并且每个枚举单元格都有一个 .Value
属性。
不会创建 PowerShell 友好的 [pscustomobject]
Import-Excel
中的
ImportExcel
所做的那样。
尝试更新电子表格本身,即它不会删除您想要跳过(省略)的行。
# Specify the license type (required in v5+ of EPPlus)
$env:EPPlusLicenseContext = 'NonCommercial'
# Load the EPPlus assembly.
Add-Type -LiteralPath "/home/lokesh/EPPlus.dll" # Replace with the path to your EPPlus.dll file
# The file path of the workbook to open.
$filePath = "/home/lokesh/NSG_Rules.xlsx"
# Create an Excel package object and load the workbook file.
# Convert-Path ensures that a *full path* is passed, which is required in
# .NET calls, given that .NET's working dir. usually differs from PowerShell's.
$excelPackage =
[OfficeOpenXml.ExcelPackage]::new((Convert-Path -LiteralPath $filePath))
# Get the first worksheet from the Excel package
$worksheet = $excelPackage.Workbook.Worksheets[0]
# Iterate over all rows that have cells with content and/or formatting
# and collect the data rows of interest.
$headerProcessed = $false
[array] $rowsOfInterest =
foreach ($row in $worksheet.Rows) { # process each row
$rowCellCount = 0; $skipRow = $false
foreach ($cell in $row.Range) { # process each cell in a row
++$rowCellCount
if (-not $headerProcessed) { continue } # header row: just count the cells.
# data row: check for a background color of interest.
$skipRow = $cell.Style.Fill.BackgroundColor.Rgb -in 'FFFFFF00', 'FFA9A9A9'
if ($skipRow) { break }
}
if (-not $headerProcessed) {
# header row: Save the count of cells in the header row (assumed to be the first one),
# i.e. the count of columns
$headerRowCellCount = $rowCellCount
$headerProcessed = $true
continue
}
# data row: Also check if not all cells in the rows had values.
$skipRow = $skipRow -or $rowCellCount -lt $headerRowCellCount
if (-not $skipRow) {
$row # output
}
}
# $rowsOfInterest is now an array of [OfficeOpenXml.ExcelRangeRow] instances
# containing only those data rows that neither have empty cells nor
# cells with the specified background colors.
# Sample output: Print the address and value of the cells in these rows.
$rowsOfInterest.Range | Select-Object Address, Value