I have been working on a function that will write worklog information to an excel template. I am able to get it to work but once i added the date column and tried to write a excel function to it I'm getting the below error.
Exception setting "Item": "Exception from HRESULT: 0x800A03EC"At C:\Users\shuppz\Documents\WindowsPowerShell\Modules\Client Config CDRL\Source\Get-Assignment.ps1:1482 char:4+ $Main.Cells.Item($i, 9) = $DateFunction+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException+ FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertySetValueTI
the excel string i need to add to the cell is to add a date and the string i created is in the below function called $DateFunction.
Function Create-Worklog
{
Param ($NewFile, $DBName, $Work)
$before = @(Get-Process [e]xcel | %{ $_.Id })
$ExcelObject = new-Object -comobject Excel.Application
$ExcelId = Get-Process excel | %{ $_.Id } | ? { $before -notcontains $_ }
$ExcelObject.visible = $False
$ExcelObject.DisplayAlerts = $False
$ActiveWorkbook = $ExcelObject.WorkBooks.Open($NewFile)
$Main = $ActiveWorkbook.Worksheets.Item('Worklog')
$Main.Cells.Item(2, 6) = $(Get-Date -f MM/dd/yyyy)
$Main.Cells.Item(3, 6) = $($DBName.Replace(".accdb",""))
$i = 6
ForEach ($W in $Work)
{
$DateFunction = "=IF (G$i = " + [char](34) +"Mitigated" + [char](34) + ", TODAY(), IF (G$i = " + [char](34) +"Escalated" + [char](34) + ", TODAY(), IF (G$i = " + [char](34) +"Offline"", TODAY(), " + [char](34) +")))"
$Main.Cells.Item($i, 1) = $W.Workstation
$Main.Cells.Item($i, 2) = $W.VulnCount
$Main.Cells.Item($i, 3) = $W.OS
$Main.Cells.Item($i, 4) = $W.AuditID
$Main.Cells.Item($i, 5) = $W.Name
$Main.Cells.Item($i, 6) = $W.Fix
$Main.Cells.Item($i, 7) = "Open"
$Main.Cells.Item($i, 9) = $DateFunction
$TBString = "{0},{1}" -f $W.Workstation, $W.AuditID
$Main.Cells.Item($i, 12) = $TBString
$i++
}
$ActiveWorkbook.SaveAs($NewFile)
$ExcelObject.Quit() | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject)
Stop-Process -Id $ExcelId -Force -ErrorAction SilentlyContinue
}
If someone can help me figure out how to add this custom excel function. When I run the function everything gets filled out but the date function.