我目前正在编写一个脚本,该脚本将跟踪不同网络服务器上的数据库大小。我将结果输出到 Excel,这将每天运行,因此,我需要知道如何获取包含数据的最后一行并从那里开始追加。
#Creates heading on an excel file if the file did not exist previously
Function CreateExcelWithHeadings($intRow, $Sheet)
{
$intRow++
#Create column headers
$Sheet.Cells.Item($intRow,1)= "Date"
$Sheet.Cells.Item($intRow,1).Font.Size = 12
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,2) = "Server"
$Sheet.Cells.Item($intRow,2).Font.Size = 12
$Sheet.Cells.Item($intRow,2).Font.Bold = $True
#$intRow++
$Sheet.Cells.Item($intRow,3) = "Database"
$Sheet.Cells.Item($intRow,3).Font.Bold = $True
$Sheet.Cells.Item($intRow,4) = "Data Name"
$Sheet.Cells.Item($intRow,4).Font.Bold = $True
$Sheet.Cells.Item($intRow,5) = "Data File"
$Sheet.Cells.Item($intRow,5).Font.Bold = $True
$sheet.Cells.Item($intRow,6) = "Data Size (MB)"
$Sheet.Cells.Item($intRow,6).Font.Bold = $True
$Sheet.Cells.Item($intRow,7) = "Data Used Space (MB)"
$Sheet.Cells.Item($intRow,7).Font.Bold = $True
$Sheet.Cells.Item($intRow,8) = "Log Name"
$Sheet.Cells.Item($intRow,8).Font.Bold = $True
$Sheet.Cells.Item($intRow,9) = "Log Size (MB)"
$Sheet.Cells.Item($intRow,9).Font.Bold = $True
$Sheet.Cells.Item($intRow,10) = "Log Used Space (MB)"
$Sheet.Cells.Item($intRow,10).Font.Bold = $True
$Sheet.Cells.Item($intRow,11) = "Log File"
$Sheet.Cells.Item($intRow,11).Font.Bold = $True
}
# initalize variables
$serverList = "C:\Database Monitor\sqlservers.txt"
$filepath ='C:\Database Monitor\Log Files\'
$filename = 'database_logging.xlsx'
#Create a new Excel object using COM
$Excel = New-Object -ComObject Excel.Application
# check if file path exists, if its doesn't create one with the proper headings
If (-not (Test-Path -path $filepath))
{
New-Item -ItemType directory -Path $filepath
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
CreateExcelWithheadings 0 $Sheet
$Sheet.SaveAs($filepath+$filename)
#WritetoExcel($serverList, 0)
}
Else
{
$Excel.visible = $True
$Excel = $Excel.Workbooks.Open($filepath)
$Sheet = $Excel.Worksheets.Item(1)
$ExcelWorkSheet.activate()
#$lastRow =
}