The actual code loads up a excel spread sheet and then maniputes it with the data that it pulls via the Rest API call, The script works comepletly fine when i run in the ISE, UI or as a batchfile, but when i try to execute the same script via the windows schduler,
- If the windows schduler calls the powershell scripts directly - The powershell process starts, but i dont see excel process in the memory, the script fails while opening the excel spreadhseet, Error exception dosent seem to report what the exception is
- If the windows schduler calls the batch file - The powershell process starts, i do see the excel loading in the memory, but still fails while loading the spreadsheet, Error exception dosent seem to report what the exception is.
Would appreciate any help on this, Thanks
##############################################################################################################
#Function Name: ErrorHandler
#Inputs: Error Type(Info, Warning or Error), Error String
#Output: None
#Description: If the error type is 'Error' then captures the last known exceptions reported and appends to the file.
# If the error type is something else then the error message is appended to the log file.
##############################################################################################################
Function ErrorHandler ($errType, $ErrorString)
{
$timestamp = Get-Date -Format o
if ($errType -eq 'Error')
{
$errorstr = $timestamp + ' : ' + $errType + $ErrorString + $Error[0].Exception
}
else
{
$errorstr = $timestamp + ' : ' + $errType + ' - ' + $ErrorString
}
$errcounter = 0
$maxerrcounter = 5
do
{
try
{
$errorstr | Out-File $ErrFile -Append
break
}
catch
{
Start-Sleep 10
$errcounter++
}
}while ($errcounter -le $maxerrcounter)
}
#########################################
#Initialization
#########################################
#Folder Name
$Fldr = 'C:\RPA\LRP_Report_Automation\'
#Error file name
$ErrFile = $Fldr + 'LPR.log'
#LPR Report master spreadsheet
$excelFile = $Fldr + "\Report\LRP_Empty.xlsb"
$xl_file_password = "test123"
$dirForRAW_CSVFiles = $Fldr + 'Report\RawData\'
#creating a hash table to store the relationship betwen the API columns and the Excel columns
$excelparamhash=@{}
ErrorHandler 'Info' "Starting the Module Import."
#########################################
#Intialize Excel object
#########################################
foreach ($user in (Get-ChildItem -Path C:\Users)) {
$Env:PSModulePath += ";$($user.FullName)\Documents\WindowsPowerShell\Modules"
}
#Get-Module "C:\Program Files\WindowsPowerShell\Modules\ImportExcel" -ListAvailable | Import-Module -Force #-Verbose
#Get-Module ImportExcel -ListAvailable | Import-Module -Force #-Verbose
Import-Module -Name ImportExcel -Force
# Load the Excel COM object
$Targetexcel = New-Object -ComObject "Excel.Application"
$Targetexcel.Visible = $false
$Targetexcel.DisplayAlerts = $false
# Open the LPR report master file
try
{
$workbook = $Targetexcel.Workbooks.Open($excelFile, $false, $false, [Type]::Missing, $xl_file_password, $xl_file_password, $true)
}
catch
{
ErrorHandler 'Error ' "Finished the Module Import."
}
#save the LRP report workbook
$workbook.Save()
#close the LRP workbook
$workbook.Close()
$Targetexcel.Quit()