# Excel file information
$excelFilePath = "I:\Jason Stephens\hpfDailyChecks.xls"
# Open Excel and display to the screen
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $excel.Workbooks.Open($excelFilePath)
# Create sheet for current year if it does not already exist
ForEach ($worksheet in $workbook.Worksheets)
{
If ($worksheet.name -eq (Get-Date -Format "yyyy"))
{
write-host "TADA!"
}
write-host $worksheet.name
}
If ($workbook.sheets | Where-Object {$_.name -eq (Get-Date -Format "yyyy")} -eq $null)
{
write-host "in if statement"
}
# Shut down excel
$excel.Quit()
As you can see, I am floundering a bit here. I have a workbook with 2 sheets named
"General" and "2008". The question is, what happens when 2009 comes around?
I would like my script to automatically create and start using the new sheet.
The first little thing I have there is a loop through all the pages. That isn't
really as smooth as the if statement that I have. I would rather have the if
statement if possible instead of using a flag after the loop to create it if it
is false. I think the problem is that my WHERE-OBJECT is returning and empty
collection. How do I deal with that?
On a side note, the excel.quit() doesn't shut down excel in my processes.
I have read about this but the solutions I have found don't seem to work.
Any help with this would also be greatly appreciated! :)