The powershell remote virtual machine cannot open the excel file

He, Erchuang/何 二创 141 Reputation points
2022-12-07T07:19:51.927+00:00

When using the powershell command to read and write the excel file on the remote Windows virtual machine, use the "Invoke-Command" command to successfully access the remote virtual machine. But when using "Excel.Application.Workbooks.Open($file_excel)" to open Excel, the command execution reports an error.
error:Cannot access file 'C:\20221128.xlsx'

However, using the "Excel.Application.Workbooks.Open($file_excel)" command on the remote machine can normally open the Excel file(Use the same login user name).

Thank you very much for your help.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,081 Reputation points
    2022-12-09T13:50:13.333+00:00

    1.>Can you open excel files under paths other than C:\?
    Can open excel open in other locations of the remote machine, such as the Windows desktop.

    I'm sure that he was referring to other folder names while using invoke-command. For testing purposes, create a folder C:\Temp, grant "everyone full control", and copy the spreadsheet to it. Test with Invoke-Command.

    Please read:

    https://learn.microsoft.com/en-us/office/client-developer/integration/considerations-unattended-automation-office-microsoft-365-for-unattended-rpa

    268936-image.png

    Years ago one of the servers that I supported ran a web site app that used Office automation for Word and Excel. I had all kinds of problems with that. We eventually gave up on Office and purchased a 3rd party app that allowed us to create .doc and .xls files.

    I would recommend that you investigate other solutions for unattended execution of Excel.

    https://devblogs.microsoft.com/scripting/introducing-the-powershell-excel-module-2/

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2022-12-07T15:58:25.763+00:00

    Does the file path you supply normally contain spaces?

    1 person found this answer helpful.

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,381 Reputation points Microsoft Vendor
    2022-12-08T07:21:53.793+00:00

    Hi @He, Erchuang/何 二创 ,

    Can you open excel files under paths other than C:\? Are you using the same user to run the Invoke-Command cmdlet as to log in to the remote virtual machine? How about explicitly specifying the user with the -credential parameter?

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.