powershell XLS 到 CSV 的转换问题

powershell XLS to CSV conversion issue

我在 运行 下面的代码中将 XLS 转换为 CSV,但出现了奇怪的错误。有人可以指出导致此问题的正确方向吗? 我正在使用 powershell v3.0。不确定我在这里遗漏了什么。

PARAM
    (
        [parameter(Mandatory = $true)]
        [string]
        $excelFilePath = $(throw "Must give a valid Excel (*.xls) file path.")
        ,

        [parameter(Mandatory = $true)]
        [int]
        $leadingRowsToDelete = $(throw "Must specify how many leading rows to delete.")
        ,

        [parameter(Mandatory = $false)]
        [string]
        $csvFilePath = $null
        ,

        [parameter(Mandatory = $false)]
        [int]
        $xlCSV = 6
    )


    if(! $csvFilePath)
    {
        $csvFilePath = $excelFilePath -replace ".xls$", ".csv"
    }

    $Excel = New-Object -Com Excel.Application -Property @{Visible = $false} 
    $Excel.displayalerts=$False

    $WorkBook = $Excel.Workbooks.Open($excelFilePath) # Open the file
    $Sheet = $WorkBook.Sheets.Item(1) # Activate the first worksheet

    # Delete the first $leadingRowsToDelete rows
    for($i = 1; $i -le $leadingRowsToDelete; $i ++)
    {
        [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
    }

    $WorkBook.SaveAs($csvFilePath, $xlCSV)

    $Excel.quit()

我收到的错误消息是:

Exception calling "Open" with "1" argument(s): "The server threw an exception. (Exception from HRESULT: 0x80010105
(RPC_E_SERVERFAULT))"
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:74 char:1
+ $WorkBook = $Excel.Workbooks.Open($excelFilePath) # Open the file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:75 char:1
+ $Sheet = $WorkBook.Sheets.Item(1) # Activate the first worksheet
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:83 char:1
+ $WorkBook.SaveAs($csvFilePath, $xlCSV)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

根据 OP 的评论,问题是由目标 Excel 文件的错误权限引起的。

脚本在具有完全权限的本地文件上运行良好。