0

I have a powershell script that is converting CSV to XLSX. It works as intended when outputting to local disk. It runs for a few seconds and then an XLSX file is generated in $files path.

$files = Get-ChildItem c:\test\*.csv
Write "Loading Files..."

$Excel = New-Object -ComObject Excel.Application

ForEach ($file in $files)
{

     $WorkBook = $Excel.Workbooks.Open($file.Fullname)
     $NewFilepath = $file.Fullname -replace ".{4}$"
     $NewFilepath =  $NewFilepath + ".xlsx"
     $Workbook.SaveAs($NewFilepath,51)
}

$Excel.Quit()  
Stop-Process -processname EXCEL

But when I change it to output to a file share, it immediately completes with no error, but no output file is generated.

$files = Get-ChildItem \\fileserver\path\*.csv
Write "Loading Files..."

$Excel = New-Object -ComObject Excel.Application

ForEach ($file in $files)
{

     $WorkBook = $Excel.Workbooks.Open($file.Fullname)
     $NewFilepath = $file.Fullname -replace ".{4}$"
     $NewFilepath =  $NewFilepath + ".xlsx"
     $Workbook.SaveAs($NewFilepath,51)
}

$Excel.Quit()  
Stop-Process -processname EXCEL

I would suspect this is a permissions issue, but the logged in account has write access the share. Does a powershell script change the user/account context? What can I add to this script to capture errors and identify the problem?

alexherm
  • 1,362
  • 2
  • 18
  • 31

1 Answers1

1

try this:

$files = Get-ChildItem "\\fileserver\path" -filter *.csv"

and then separately echo out after your $NewFilepath assignments.

If that doesn't give you more info, then try writing something simple to the share like:

"Hello World" > "\\fileserver\path\test.txt"

That could give you clues on user context and permissions.

adamt8
  • 308
  • 1
  • 7