5

I am running the script below as a scheduled task with the user logged on the server. It converts an xls file to csv using the Excel.Application COM object. The conversion works, but eventually breaks and I don't know why.

I have the task run the following command which should in theory allow it to run constantly:

powershell.exe -noexit -file "filename.ps1"

Any thoughts on what to try?

$server = "\\server"
$xls = "\path\XLS\"
$csv = "\path\CSV\"
$folder = $server + $xls
$destination = $server + $csv

$filter = "*.xls" # <-- set this according to your requirements

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true # <-- set this according to your requirements
    NotifyFilter = [IO.NotifyFilters]"FileName, LastWrite"
}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    $excelFile = $folder + $name
    $E = New-Object -ComObject Excel.Application
    $E.Visible = $false
    $E.DisplayAlerts = $false
    $wb = $E.Workbooks.Open($excelFile)

    foreach ($ws in $wb.Worksheets) {
        $n = "output_" + $name -replace ".XLS"
        $ws.SaveAs($destination + $n + ".csv", 6)
    }

    $E.Quit()     
}
10
  • 1
    Are you running the task as a user who has the rights to access the network server? Commented Jun 30, 2017 at 17:02
  • 1
    "The converting works, but eventually breaks and I don't know why." Can you add more detail? Which part works, and which part breaks? Does it work once and then fails 2nd time or works a few times and then breaks? Does it matter whether the user is logged in or not? Task screenshots/settings would be helpful Commented Jun 30, 2017 at 17:03
  • As Jeff said, verify the user the task is running as. There is a field in scheduled tasks that allows you to enter a user and password to run as. If it "partially" runs, it may be that it doesn't have access to a certain directory you are trying to convert Commented Jun 30, 2017 at 17:22
  • 2
    @gms0ulman - converting happens. i drop a .xls file into the folder, it gets converted to .csv because the script is running in the background. i just notice eventually usually 4-5 days later, when i drop a .xls file, the convert stops even though the powershell script is still running. Commented Jun 30, 2017 at 17:25
  • 1
    I guess you should have your script increment a log file to know which part fails? Commented Jun 30, 2017 at 17:51

1 Answer 1

1

I was doing something similar with word. I couldnt use Quit alone. I think using Quit hides Excel. Try releasing the com object by using:

$E.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($E)
Remove-Variable E

i dont know if you are opening the Excel application but if you are you can maybe use

$wb.close($false)

Let me know if it works...

Ref: https://technet.microsoft.com/en-us/library/ff730962.aspx?f=255&MSPPError=-2147217396

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.