当用户登录时,为什么在powershell中以非交互方式使用Excel com会失败?

当用户登录时,为什么在powershell中以非交互方式使用Excel com会失败?,excel,powershell,com,Excel,Powershell,Com,我有一个powershell函数,它使用excel com对象将XLS转换为CSV。 忽略MS_logoutput这只是一个自定义输出格式化程序函数 convert函数在脚本中由自动调度程序调用 计划程序正在Domain\HOST1上运行,并以Domain\User1的身份启动powershell进程。 它以非交互方式运行此操作 直到最近,此作业一直在运行,没有问题,但是,最近,如果有一个与user Domain\User1的VM Domain\HOST1的登录会话,则此作业的自动非交互运行开始

我有一个powershell函数,它使用excel com对象将XLS转换为CSV。 忽略MS_logoutput这只是一个自定义输出格式化程序函数

convert函数在脚本中由自动调度程序调用

计划程序正在Domain\HOST1上运行,并以Domain\User1的身份启动powershell进程。 它以非交互方式运行此操作

直到最近,此作业一直在运行,没有问题,但是,最近,如果有一个与user Domain\User1的VM Domain\HOST1的登录会话,则此作业的自动非交互运行开始失败。 (此会话可以登录并断开,但问题仍然存在)

DCOM配置的安全性全部设置为自定义,并且Domain\User1用户对所有权限具有完全控制权

标识设置为“启动用户”

职能:


Function MS_Convert-XLStoCSV
{
    Param(
        [string] $ExcelInputFilepath = "$InputPathBOJ\$($ROW.InputFileBOJ).xls",
        [string] $CsvlOutputFilepath = "$InputPathBOJ\$($ROW.InputFileBOJ).csv",
        $SheetIndexToSave,
        $DeleteFirstRowsCount
        

        
    )



    Begin
    {
        MS_logoutput "Function:", $MyInvocation.MyCommand, "Started"
        
    }
  
    Process
    {
        Try
        {
            & {
                if ($null -eq $SheetIndexToSave)
                {
                    $SheetIndexToSave = 1 
                }
                if ($null -eq $DeleteFirstRowsCount)
                {
                    $DeleteFirstRowsCount = 0
                }
                MS_logoutput "Adding Excel Type"
                $excel = New-Object -comobject Excel.Application
                MS_logoutput "Added Excel Type succesfully"
                $excel.DisplayAlerts = $false
                #$excel.Visible = $false

                MS_logoutput "Opening $ExcelInputFilepath with Excel"
                $OpenWorkbook = $Excel.Workbooks.Open("$ExcelInputFilepath")
                MS_logoutput "Workbook opened correctly"
                $OpenWorkbook.ConflictResolution = 2
                MS_logoutput "Selecting sheet at index  $SheetIndexToSave"
                $sheetToSave = $OpenWorkbook.Sheets | Where-Object { $_.Index -eq $SheetIndexToSave }
                MS_logoutput "Starting to save sheet"
                while ($DeleteFirstRowsCount -gt 0) 
                {
                    $null = $sheettosave.cells.item(1, 1).EntireRow.Delete()
                    $DeleteFirstRowsCount--
                }
                $sheetToSave.SaveAs($CsvlOutputFilepath, 6)
                MS_logoutput "File saved to $CsvlOutputFilepath"
                MS_logoutput "Closing  Excel"
                $OpenWorkbook.Close($false)
                $excel.Quit()
            }
            $null = [System.GC]::Collect()
            MS_logoutput "Excel Closed"
              

        
        }
        Catch 
        {
            MS_logoutput "Function:", $MyInvocation.MyCommand, "Failed with exception:" "Error"
            MS_logoutput "Exception: $_" "Error"
            MS_LogOutput "Exception: $($_.Exception.Message)" "Error"
            MS_LogOutput "Inner Exception: $($_.Exception.InnerExceptionMessage)" "Error"
            MS_LogOutput "StackTrace : $($_.Exception.StackTrace)" "Error"
            
            $ErrorActionPreference = "SilentlyContinue"
            $OpenWorkbook.Close($false)
            $excel.Quit()

            $null = [System.GC]::Collect()
            $null = [System.GC]::WaitForPendingFinalizers()

            $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($OpenWorkbook)
            $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)

            $null = [System.GC]::Collect()
            $null = [System.GC]::WaitForPendingFinalizers()


            $null = Remove-Variable -Name excel
            $ErrorActionPreference = "Continue"

            throw "Exception: $_"
        }
    }
  
    End
    {
    
        MS_logoutput "Function:", $MyInvocation.MyCommand, "Ended "
        
    }


}


我得到的错误输出是

2020-07-20 19:25:19 | Info     | Function: MS_Convert-XLStoCSV Started
2020-07-20 19:25:19 | Info     | Adding Excel Type
2020-07-20 19:25:19 | Info     | Added Excel Type succesfully
2020-07-20 19:25:19 | Info     | Opening \\networklocation\some\subfolders\InputExcel.xls with Excel
2020-07-20 19:25:24 | Error    | Function: MS_Convert-XLStoCSV Failed with exception:
2020-07-20 19:25:24 | Error    | Exception: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
2020-07-20 19:25:24 | Error    | Exception: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
2020-07-20 19:25:24 | Error    | Inner Exception: 
2020-07-20 19:25:24 | Error    | StackTrace :    at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Management.Automation.ComInterop.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, ComMethodDesc method, Object[] args, UInt32 argErr)
   at CallSite.Target(Closure , CallSite , ComObject , String )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)


MS Office COM对象因无法从计划任务正常工作而臭名昭著。看看是否有帮助。@Bill_Stewart谢谢,但我都试过了。这就是几年前让这个脚本运行的问题;我怀疑这与用户登录时脚本在交互式窗口站中运行这一事实有关,但我仍然不认为它会失败;这听起来有点含糊不清:一个实用的解决办法是使用一个从不以交互方式登录的专用用户帐户。