I am having a weird problem with powershell. I have other scripts similar to the one throwing an error on a line I am using several times throughout the script.
I am using the get-content
to grab a CSV file. Then I begin to parse through that data only selecting certain columns and outputting them into excel. Here is where one of my problems occurs...
It is very inconsistent, but at some point in time while parsing through the csv I get this error
Exception setting "Item": "Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))"
On this line, which is used to output the data into excel at a specific column and row.
$excel.Cells.Item <<<< ($rowMonth, $columnMonth) = "$item"
I have noticed though, if I hold the scroll bar whilst parsing through the data, I pass over this error. I also noticed it stopped at a different spot almost every time (give or take a few cells of data), or sometimes I don't get the error at all.
Immediately after I get this error for each failed insert I get this error
Assignment failed because [System.__ComObject] doesn't contain a settable property 'Item()'.
...until I am done error out on the parsing section of the script.
I have tried to Google these errors, and there is very little information on PowerShell with Excel already and I'm having a hard time trying to figure out what the problem is (day 3..)
I have 4 other scripts that run this line of code..
$excel.Cells.Item($rowNumber, $column) = "$item"
and they have never show me this error.
Once my parsing has completed (if it get's there, I cheat while I'm trying to debug and hold the scroll bar) I have this line here which I use to switch to another worksheet in excel, and it will error out every single time here.
$sheet2 = $workBook.WorkSheets.Item($sheetName2).Select()
Also giving me the first error I had posted..Also when I do get the error whilst parsing half though, soon as it errors, excel asks me to set the book to read/write (...again...only sometimes.....).
What could be the problem here, why is it so inconsistent? Could it be memory limits? (I have parsed waaay more data in the CSV than I am in the script and have never had this issue)
Please Help SO!
Here is how I am creating my excel object.
#Create an instance of Excel
$excel = New-Object -comobject Excel.Application
#declaring sheet names
$sheetName = "some sheet name"
#open Excel file
[string]$file = "C:\Users\Desktop\excel-file.xlsx"
$workBook = $excel.Workbooks.open($file)
$workBook.WorkSheets.Item($sheetName).activate()
$excel.Visible = $true
$excel.displayAlerts = $false
Here is where I am outputting into excel:
#Declaring variables
$i = 0
$rowNumber = 2
$rowMonth = 2786
$data1 = Get-Content $file1
foreach($row in $data1){
if($row){
$line = $row.Split(",")
$i++
#reset back to the first column once there is no more data for that row
$column = 1
$columnMonth = 7
$dataColumn = 0
if($i -ge 1){
foreach($item in $line){
$dataColumn++
if($dataColumn -eq 1 -or $dataColumn -eq 2 -or $dataColumn -eq 5 -or $dataColumn -eq 7){
$excel.Cells.Item($rowNumber, $column) = "$item"
$excel.Cells.Item($rowMonth, $columnMonth) = "$item"
$column++
$columnMonth++
}#endif
}#end foreach
$rowNumber++
$rowMonth++
}#end if $i
}#end if $row
}#end foreach
I found out what my problem was:
In the script I am opening a workbook, checking a cell for a date than comparing that date to the current date. Once I get the difference, I know now how many times I must loop through my report and close the workbook. Than I begin a batch script, which collects a bunch of data for the last 15 days, I then get using a (switch). Once I have my data, I open up excel once again, and begin my reporting.
The issue was the time between closing and reopening excel, I was reopening the same workbook far to quickly before it had fully closed. So whilst parsing in my data, the workbook would than become "available" and interrupt my script throwing these errors.
I figured this out by putting a Start-Sleep -sec 20
before opening excel for the second time, and has yet to give me any issues.
Edit: I have gone and fixed my use of objects correctly. Also, I have adjusted my code to only open excel once, instead of the open-close, open-close that I had been doing.
I would not be using select or activate, instead, create objects for each sheet, and call the cells.item on the individual sheets, not the entire workbook.
So first do $Sheet1 = $WorkBook.Worksheets.Item(1)
which creates a worksheet object of the first sheet. Then add your items using $sheet1.Cells.item($row,$col) = $item
There are many sources on why not to use select or activate. Here's one : How do i activate a specific workbook and a specific sheet?
The biggest reasons you should not use it is because of performance, and mainly, it is not necessary.
User contributions licensed under CC BY-SA 3.0