如何在 PowerShell 中修复无法索引到空数组
How to fix Cannot index into a null array in PowerShell
我正在尝试将我的 CSV 文件转换为具有某些 Table 格式和样式的 Excel 文件,但由于某种原因我得到“无法索引到空数组”。如果我能得到任何帮助或建议,我将不胜感激。谢谢
function Convert-to-Excel{
$params = @{
AutoSize = $true
TableStyle = 'Medium6'
BoldTopRow = $true
WorksheetName = 'Audit Log'
PassThru = $true
Path = "C:\AuditLogSearch$((Get-Date).AddDays(-7).ToString('yyyy-MM-dd')) _ $(Get-Date -Format "yyyy-MM-dd") Audit-Log-Records11.xlsx"
}
$modifiedFile = Import-Csv "C:\AuditLogSearch\Modified-Audit-Log-Records.csv"
$actionReference = Import-Csv "C:\AuditLogSearch\Reference\Action.csv"
$xlsx = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
"Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Type of Action" = if (($actionReference | where-object { $_.Name -eq $u."Type of Action" }).Value -ne $null) { ($actionReference | where-object { $_.Name -eq $u."Type of Action" }).Value }
else { $u."Type of Action" }
} | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
}
}
您将在循环的第一次迭代中关闭 Excel 包,因此为什么当它进入下一个迭代时它试图做这样的事情:
$null[$null] # => InvalidOperation: Cannot index into a null array
尝试修改您的函数,使其看起来像这样:
- 首先,构建
object[]
:
$result = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
"Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Type of Action" = if (($actionReference.........
else { $u."Type of Action" }
}
}
- 然后导出到Excel:
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
需要注意的一点是,$params
上的 PassThru = $true
意味着我们不想直接保存 Excel,而是希望将对象保存在变量上以供“进一步操作”,并通过进一步操作我的意思是,在这种情况下,隐藏工作表的 GridLines ($ws.View.ShowGridLines = $false
),然后关闭包(将其存储在磁盘上)。
如果您不需要对工作表进行任何修改,您可以完全删除 PassThru
并执行:
$result | Export-Excel @params
这将关闭包并将 Excel 存储在您的磁盘上。
我正在尝试将我的 CSV 文件转换为具有某些 Table 格式和样式的 Excel 文件,但由于某种原因我得到“无法索引到空数组”。如果我能得到任何帮助或建议,我将不胜感激。谢谢
function Convert-to-Excel{
$params = @{
AutoSize = $true
TableStyle = 'Medium6'
BoldTopRow = $true
WorksheetName = 'Audit Log'
PassThru = $true
Path = "C:\AuditLogSearch$((Get-Date).AddDays(-7).ToString('yyyy-MM-dd')) _ $(Get-Date -Format "yyyy-MM-dd") Audit-Log-Records11.xlsx"
}
$modifiedFile = Import-Csv "C:\AuditLogSearch\Modified-Audit-Log-Records.csv"
$actionReference = Import-Csv "C:\AuditLogSearch\Reference\Action.csv"
$xlsx = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
"Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Type of Action" = if (($actionReference | where-object { $_.Name -eq $u."Type of Action" }).Value -ne $null) { ($actionReference | where-object { $_.Name -eq $u."Type of Action" }).Value }
else { $u."Type of Action" }
} | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
}
}
您将在循环的第一次迭代中关闭 Excel 包,因此为什么当它进入下一个迭代时它试图做这样的事情:
$null[$null] # => InvalidOperation: Cannot index into a null array
尝试修改您的函数,使其看起来像这样:
- 首先,构建
object[]
:
$result = foreach ($u in $modifiedFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
New-Object PsObject -Property @{
User = $u.User
"Search Criteria" = $u."Search Criteria"
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Type of Action" = if (($actionReference.........
else { $u."Type of Action" }
}
}
- 然后导出到Excel:
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.View.ShowGridLines = $false # => This will hide the GridLines on your file
Close-ExcelPackage $xlsx
需要注意的一点是,$params
上的 PassThru = $true
意味着我们不想直接保存 Excel,而是希望将对象保存在变量上以供“进一步操作”,并通过进一步操作我的意思是,在这种情况下,隐藏工作表的 GridLines ($ws.View.ShowGridLines = $false
),然后关闭包(将其存储在磁盘上)。
如果您不需要对工作表进行任何修改,您可以完全删除 PassThru
并执行:
$result | Export-Excel @params
这将关闭包并将 Excel 存储在您的磁盘上。