gpt4 book ai didi

excel - 当不同计算机访问虚拟机时,托管虚拟机上的 azure devops 代理的不同行为

转载 作者:行者123 更新时间:2023-12-03 03:09:55 25 4
gpt4 key购买 nike

编辑:经过大量测试,我们设法找到了机器之间的一个共同点,当使用 RDP 连接到我们的环境时,该共同点破坏了从 azure devops 启动的 powershell 脚本调用的 excel 功能的执行代理:所有这些机器都有触摸屏,没有触摸屏的机器不要破坏它(尝试了 3 台带触摸屏的机器,不同的构建者和 4 台没有触摸屏的机器)

编辑 2:当触摸屏被禁用并通过 RDP 连接到计算机时,不再破坏脚本

我正在使用 AzureDevops 代理在云托管环境上执行 powershell 任务,该任务非常简单,只需创建一个工作簿并保存它:

try{
write-host 'initalized excel'
$Excel = New-Object -ComObject Excel.Application
write-host 'adding WB'
$Workbook = $Excel.Workbooks.Add()
write-host 'Saving Wb'
$Workbook.SaveAs('C:\test\test.xlsx')
}
finally{
write-host 'quitting excel'
$Excel.quit()
}

这是 devops 任务的输出:

##[section]Starting: PowerShell Script
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.151.2
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\agent\_work\_temp\33ed6d09-53ba-427a-a7ac-ea425a98767c.ps1'"
initalized excel
adding WB
quitting excel
Exception from HRESULT: 0x800A03EC
At C:\agent\_work\_temp\33ed6d09-53ba-427a-a7ac-ea425a98767c.ps1:6 char:5
+ $Workbook = $Excel.Workbooks.Add()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

这就是当我的同事连接到代理执行脚本的虚拟机时发生的情况。我的同事是用windows远程桌面工具连接的。

当我使用 RDP 连接到虚拟机时,它工作正常:

##[section]Starting: PowerShell Script
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.151.2
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\agent\_work\_temp\436b2d56-2a41-4f75-97bb-01d4c41407e6.ps1'"
initalized excel
adding WB
Saving Wb
quitting excel
##[section]Finishing: PowerShell Script

我的同事和我使用相同的用户 RDP 到虚拟机。

我们尝试在另一台虚拟机上使用另一个 DevOps 代理,但出现了同样的问题。

我有点迷失,我根本不知道如何解决这个问题,如果你对标题有任何其他建议,我会采纳每一条建议。

编辑:

我们还尝试将此脚本放入一个循环中,并写入是否成功或失败,当我 rdp 到虚拟机时,它可以工作,如果我断开连接,它可以工作,只要我的同事 RDP 进入虚拟机,它就会启动失败,如果我重新连接(从而断开我的同事的连接),它会再次工作。 (我们使用同一用户进行 RDP)

编辑2:我收到了注意到我的人的编辑建议:

From the user's comment->' when the agent is already running and creating excel files in a loop, the simple fact my colleague rdp to the vm breaks the excel file creation.' This issue seems to have nothing to do with azure devops. The reason for the problem is due to rdp

这也是一个 azure devops 代理问题,因为简单地手动运行脚本对我们双方都有效。这是 RDP 问题导致 Devops 代理无法使用某些 excel-powershell 功能,所以我认为 Azure Devops 标记仍然准确

编辑:

In the finally block try outputting the full error stack with: $Error | Format-List -Force – HAL9256

错误如下:

Exception             : System.Runtime.InteropServices.COMException (0x800706BE): The remote procedure call failed. 
(Exception from HRESULT: 0x800706BE)
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 )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
at
System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
frame)
TargetObject :
CategoryInfo : OperationStopped: (:) [], COMException
FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, C:\agent\_work\_temp\fae3ef7a-700d-49c1-8f1f-7f7d2bc0d49e.ps1: line 8
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
PSMessageDetails :

最佳答案

Running Excel under an automated, unattended process (such as an Azure DevOps Agent) in unsupported and can lead to all kinds issues ,就像您遇到的那样。

官方建议是依赖第三方组件,例如 Excel Package Plus (free) , NPOI (free) , SpreadSheetGear (Commercial)Aspose Cells.NET (Commercial) 。或者使用 System.IO.Packaging 和 edit the XML for the Office document directly 。这些不依赖于您的服务器上安装了 Excel,实际上受到支持,并且不存在与之相关的各种许可证问题。将脚本迁移到这些其他包类型通常并不困难,因为它们采用非常相似的 API 来实现可移植性。

除了您遇到的技术问题之外,许可问题可能更加棘手,因为 Office 不允许“代表其他用户”实现自动化,并且通常不允许在服务器上运行。

Besides the technical problems, you must also consider licensing issues. Current licensing guidelines prevent Office applications from being used on a server to service client requests, unless those clients themselves have licensed copies of Office. Using server-side Automation to provide Office functionality to unlicensed workstations is not covered by the End User License Agreement (EULA).

https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office

您也许能够解决一些问题,例如您现在遇到的问题,但您可能会遇到新的问题。以交互方式运行 Azure DevOps 代理可能会有所帮助,这样 Excel 也将在交互 session 中启动,而不是在没有真正桌面的 protected 隐藏用户 session 中启动。可能发生的情况是,通过 RDP 连接的第二个用户在登录时关闭所有其他 session ,或者在注销时锁定所有 session 。

here are security risks when you enable automatic logon or disable the screen saver because you enable other users to walk up to the computer and use the account that automatically logs on. If you configure the agent to run in this way, you must ensure the computer is physically protected; for example, located in a secure facility. If you use Remote Desktop to access the computer on which an agent is running with auto-logon, simply closing the Remote Desktop causes the computer to be locked and any UI tests that run on this agent may fail. To avoid this, use the tscon command to disconnect from Remote Desktop. For example:

 %windir%\System32\tscon.exe 1 /dest:console

https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/agents?view=azure-devops#interactive-or-service

有关服务器上 Excel 自动化的另一个问题是重复使用现有的 Excel 实例。这可能会导致主机 excel 在与执行自动化的进程不同的凭据下运行。

<小时/>

要以交互方式运行代理,请将服务配置为 auto-logon ( this utility encrypts the credentials used ) 和 add run.cmd in the agent folder to the start-up executable list (either in the registry or a shortcut in the start menu )。 You can also make the agent executable (c:\windows\system32\cmd.exe /c path\to\agent\run.cmd) the default shellThere are quite a few docs on setting a custom shell .

您还需要为此 session 禁用屏幕保护程序和锁屏,这也需要您以物理方式保护服务器,否则任何有权访问服务器控制台的人都将能够访问正在运行的交互式 session 。

并且您必须确保始终断开远程桌面 session 而不锁定它(如上所述)。

关于excel - 当不同计算机访问虚拟机时,托管虚拟机上的 azure devops 代理的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com