XLS to CSV via powershell

HI,

I am doing the following to convert a xls file to csv file:

$tmp = C:\programdata\download.xls
$objExcel = New-Object -ComObject Excel.Application 
    if ((test-path $tmp) -and ($tmp -match ".xl\w*$")) 
        {
        $path = (resolve-path -Path $tmp).path 
        $savePath = $tmp -replace ".xl\w*$",".csv"
        if(Test-path $savePath)
            {
            Remove-Item -Path $savePath -Force | Out-Null
            }
        $objworkbook=$objExcel.Workbooks.Open($tmp)
        $objworkbook.SaveAs($savePath,6) # 6 is the code for .CSV 
        $objworkbook.Close($false) 
        }

Doing this normal via powershell it works fine
But if I use a schedule Task using

powershell.exe -file C:\data\myscript.ps1 -executionpolicy Unrestricted

the csv is not created.
I already tried I have write rights to the folder and the script is started correctly.

Any idea why this could fail?
By printing out $objExcel > $env:APPDATA\objExcel.txt I get a txt with type of $objExcel
By printing out $objworkbook > $env:APPDATA\objWorkbook.txt I get only an empty objWorkbook.txt :-(

Could this be a problem?

Thank you for help

MK-Maddin

August 27th, 2013 3:12pm

Seems that you've lost the quotation marks. Please change the first sentense like this:

$tmp = "C:\programdata\download.xls"

"But if I use a schedule Task using"

- I recommand that we open the notepad, type the following content and then saved as XXX.bat

powershell.exe -file C:\data\myscript.ps1 -executionpolicy Unrestricted

After that, please directly run this XXX.bat in the Task Scheduler and it would work fine. Thanks.

Tony Chen
TechNet Community Support

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2013 2:49am

Hey,

thank you for reply.

The quotation marks I lost during copy into the post :-)

I tried with the bat using cmd /C C:\data\myscript.bat
Once normal and second using schedule Task.
Normal everything works fine. But if I use the same parametersin schedule Task csv is still not created.

I don't understand why :(

Regrads

MK-Maddin

August 28th, 2013 9:04am

Could you pleae detail the steps how you set the task in Task Scheduler?

In my site, it works fine every time when I run it.

Tony Chen
TechNet Community Support

Free Windows Admin Tool Kit Click here and download it now
August 29th, 2013 10:04am

Hey,

thank you for your help.
I although tried to run the script as a service in background.. and i have exactly the same issue.

The convertion is not done. This is why I don't think its a problem of the schedule task...

Doing some more tests, what I found is that the line:

$objworkbook=$objExcel.Workbooks.Open($tmp)

is not creating the $objworkbook. $objExcel is created successfully.
So I know where exactly the problem is.
Printing out the $Error variable I got the following message:

Exception calling "Open" with "1" argument(s): "Microsoft Excel cannot access the file 'C:\programdata\download.xls'. There are several possible reasons:
 The file name or path does not exist.
 The file is being used by another program.
 The workbook you are trying to save has the same name as a currently open workbook."

I will check these 3 points, while I am having some more time and give information. :-)


September 1st, 2013 2:11pm

Hi,

I the three points now.

The file name or path does not exist.

Of course the file not already exists, because I try to save. But path is all correct.

 The file is being used by another program.

I checked this with the program "Unlocker" but did not get a usage by any other program.

 The workbook you are trying to save has the same name as a currently open workbook."

I have no idea how tho check this :-(
The worbook is not even created..so how should i open it with the same name?

At the moment I have no idea how to go on.

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2013 8:19am

:-)

I found a solution.
It's provided here:

http://www.gregorystrike.com/2010/05/27/how-to-automate-excel-using-scheduled-tasks-on-windows-2008-x64/

Thank you to all for your help

  • Marked as answer by MK-Maddin Wednesday, September 04, 2013 6:31 AM
September 4th, 2013 6:31am

For convenience, the link above basically states that Excel automation in scheduled tasks requires the executing host to have the following folder present depending upon the OS version and architecture and Office Automation (x86/x64) installed:
  C:\Windows\System32\config\systemprofile\Desktop
  C:\Windows\SysWOW64\config\systemprofile\Desktop

Once created, the tasks should run fine.

Free Windows Admin Tool Kit Click here and download it now
June 10th, 2015 4:42pm

For convenience, the link above basically states that Excel automation in scheduled tasks requires the executing host to have the following folder present depending upon the OS version and architecture and Office Automation (x86/x64) installed:
  C:\Windows\System32\config\systemprofile\Desktop
  C:\Windows\SysWOW64\config\systemprofile\Desktop

Once created, the tasks should run fine.

June 10th, 2015 4:42pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics