Difference between powershell and remote powershell

Artimes 346 Reputation points
2021-02-22T15:54:39.143+00:00

If I run the commands below in a local powershell window its working as expected. And if I login remotly to the same machine via Enter-PSSession the commands are not working. I started both powershell sessions in same user context. The output of $Word.Visible is still "False" in remote session, also the other commands based on $Word doesn't effect. Why I can't control word in a remote powershell session? It seems that this problem is not new but I can't find a solution.

$Word = New-Object -ComObject Word.Application
$Word.Visible = $true
$OpenFile = $Word.Documents.Open("mydoc.dotx")
$OpenFile.Close()
$Word.Quit()

Same for c# console program that try work with com interface.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,519 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,657 questions
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,434 questions
{count} votes

5 answers

Sort by: Most helpful
  1. MotoX80 32,531 Reputation points
    2021-02-22T22:09:43.187+00:00

    It's not supported.

    https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2

    Years ago our app teams built web sites that would use the COM interface to Word and Excel. I had to schedule a task to periodically kill hung processes on my IIS servers. Office apps expect to interact with a real user who can do things like "click ok to continue".

    We purchased OfficeWriter and converted the sites. https://support.softartisans.com/support-95.aspx

    I don't know if any open source solution exist or not.

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 100.4K Reputation points MVP
    2021-02-22T16:12:17.637+00:00

    The answer is given in your linked stackoverflow thread:

    In a remote session there is no GUI environment.

    At least that's what I think is the reason.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 32,326 Reputation points Microsoft Vendor
    2021-02-23T09:37:51.083+00:00

    Hi,

    Enter-PSSession only supports console applications, not GUI. If you want to run a GUI program on remote computers you can try PsExec with -i parameter.
    https://learn.microsoft.com/en-us/sysinternals/downloads/psexec

    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.

    0 comments No comments

  4. MotoX80 32,531 Reputation points
    2021-02-28T16:42:02.26+00:00

    But why we can't use the com interface in a remote shell?

    You are not in an interactive (desktop) session. From the link that I provided.

    Interactivity with the desktop: Office applications assume that they are being run under an interactive desktop. In some circumstances, applications may need to be made visible for certain Automation functions to work correctly. If an unexpected error occurs, or if an unspecified parameter is needed to complete a function, Office is designed to prompt the user with a modal dialog box that asks the user what the user wants to do. A modal dialog box on a non-interactive desktop cannot be dismissed. Therefore, that thread stops responding (hangs) indefinitely. Although certain coding practices can help reduce the likelihood of this issue, these practices cannot prevent the issue entirely. This fact alone makes running Office Applications from a server-side environment risky and unsupported.

    Have you checked the eventlogs for messages? You could try tracing it with Process Monitor. The files and registry keys it accesses might give you a clue as to what it's doing.

    https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

    Windows used to have the capability to log msgbox alerts and to provide a default reply. But I don't think that works on newer versions. You could try that too.

    https://social.msdn.microsoft.com/Forums/Windows/en-US/9978975e-32d5-4f31-b874-fead3b318e3e/message-boxes-in-application-never-display?forum=quebeccomponentsforum
    https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms940850(v=winembedded.5)?redirectedfrom=MSDN

    reg add  "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Error Message Instrument" /f   
    reg add  "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Error Message Instrument" /v EnableLogging /t  REG_DWORD  /d  1 /f  
    reg add  "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Error Message Instrument" /v LogSeverity /t  REG_DWORD  /d  0 /f  
      
    

  5. MotoX80 32,531 Reputation points
    2021-03-03T00:10:13.97+00:00

    Try putting in the full path to the file, and use a .doc or .docx file.

    I have an old version of Word installed on a desktop (bigblue). From my laptop it is working.

    II logged on to the desktop and created c:\temp\test.doc. I just added some characters and saved it.

    I used this script from my laptop.

     $Word = New-Object -ComObject Word.Application
     "Got Word"
     $Word.Visible = $false
     $OpenFile = $Word.Documents.Open("c:\temp\test.doc")
     $OpenFile | format-list 
     "Its open, sleeping"
     start-sleep 10
     $OpenFile.Close()
     $Word.Quit()
     "Done"
    

    [bigblue]: PS C:\temp> .\wrd.ps1
    Got Word

    Name : test.doc
    Application : System.__ComObject
    Creator : 1297307460
    Parent : System.__ComObject
    BuiltInDocumentProperties : System.__ComObject

    .... lines removed for brevity ...

    FormattingShowFilter : 0

    Its open, sleeping
    Done

    0 comments No comments