So, using Export-Excel, and dbatools, I'd like to pipe data from one loop to one column.
All of the examples I've found so far, only show data you get from i.e. Get-Service
Get-Service | Select-Object -Property Name, Status, DisplayName, ServiceName
Which is going to create data on columns A, B, C and D on row 2, etc.
I'm running multiple foreach loops:
# Check if SQL Server Agent is running
foreach ($instance in $sqlInstanceConfig) {
$agent = Get-DbaAgentServer $instance
$availabilityGroup = Get-DbaAvailabilityGroup -SqlInstance $instance
if ($agent.SqlServerRestart) {
Write-Log "INFO" "Agent is running on $instance." $ExecutionLogFullPath
}
elseif (!$agent.SqlServerRestart) {
Write-Log "WARNING" "Agent is not running on $instance." $ExecutionLogFullPath
}
# Grab availability groups, and databases inside the groups
foreach ($group in $availabilityGroup) {
Write-Log "INFO" "For instance $($group.SqlInstance), Name: $($group.AvailabilityGroup) | Role: $($group.LocalReplicaRole) | Databases: $($group.AvailabilityDatabases)" $ExecutionLogFullPath
}
}
I've also named the excel column "headers" to my liking:
$exportExcel = Export-Excel -Path "C:\Users\janb\Desktop\testExport.xlsx" -ClearSheet -WorksheetName "TEST 123" -PassThru
$ws = $exportExcel.Workbook.Worksheets["TEST 123"]
$ws.Cells["A1"].Value = "SQL Instance"
$ws.Cells["B1"].Value = "Instance status"
$ws.Cells["C1"].Value = "Server agent status"
$ws.Cells["D1"].Value = "DB disk space"
$ws.Cells["E1"].Value = "DB in AG / Synchronizing"
$ws.Cells["F1"].Value = "Primary replica"
$ws.Cells["G1"].Value = "DB full & log backup"
$ws.Cells["H1"].Value = "Log backup oversize"
$ws.Cells["I1"].Value = "Backup disk space"
$ws.Cells["J1"].Value = "Job name"
$ws.Cells["K1"].Value = "Jobs correctly enabled"
$ws.Cells["L1"].Value = "Enabled job running / Last run date"
But now I'm stuck on piping data to A2->A*, B2->B*, etc. based on the number of results I get from my previous loops.
So what I'm trying to accomplish is, how do I write data from $instance to A2 and down without hardcoding the column values, because they can change at any time.