【问题标题】:Unable to come out of the powershell script无法脱离powershell脚本
【发布时间】:2022-01-23 15:17:24
【问题描述】:

我有以下用 powershell 编写的代码,我正在调用一个 vbs 脚本并尝试安装一个运行良好但发布脚本永远不会自动关闭的服务

CScript.exe "C:\Program Files\AppDynamics\machineagent-bundle-64bit-windows-21.12.0.3201\UninstallService.vbs"

下面是输出

Attempting to Stop Machine Agent Service
Stopping service 'Appdynamics Machine Agent'.
Service stopped

Uninstalling AppDynamics Machine AgentService from the Service Manager
Service is already stopped.
Uninstalled service 'Appdynamics Machine Agent'.

Done.
Removing Machine Agent VM options

Done.

在 tgis 之后,屏幕仍然在这里,并且它永远不会退出,直到我没有提到退出。所以我想将此步骤集成到正在工作的管道中,但管道会继续运行,因为它永远不会出现。

【问题讨论】:

  • 是永远不会关闭的 CScript.exe 还是永远不会关闭的调用 CScript.exe 的窗口?
  • @Otter 调用 Cscript.exe 的窗口永远不会关闭。所以我从powershell调用它,执行后它不会自行关闭
  • 然后我会看看管道是如何执行命令的,因为理论上这对于调用任何脚本/文件都是一个问题,而不仅仅是 CScript。你用什么来执行这个?
  • @Otter 我正在尝试卸载 appdynamics 机器代理服务。因此,如果从 powershell ISE 执行此操作,我也面临这个问题,它永远不会停止执行
  • @Otter 有没有办法在 10 秒内执行这个脚本然后从这里退出

标签: powershell


【解决方案1】:

试试这个。它创建一个它监控的后台作业。一旦它检测到Removing Machine Agent VM options,它就会翻转一个布尔值,使Done. 的下一个实例终止后台作业,同时终止该进程。

function Watch-UninstallMA {
  param([Parameter(Mandatory,ValueFromPipeline)][Management.Automation.Job]$vbs)
  :monitor do {
  foreach ($line in ((Receive-Job $vbs) -split "`n")) {
    Write-Output $line
    if ($line.Trim() -eq 'Removing Machine Agent VM options') { $removedvmo = $true }
    if ($line.Trim() -eq 'Done.' -and $removedvmo) { break monitor }
  }}
  while ($vbs.HasMoreData -and $vbs.State -eq 'Running')
  Remove-Job $vbs -Force
}

Start-Job { cscript.exe ($env:PROGRAMFILES +
"\AppDynamics\machineagent-bundle-64bit-windows-21.12.0.3201\UninstallService.vbs")
} | Watch-UninstallMA

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2021-05-13
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2015-05-26
    • 2016-07-13
    • 2013-01-02
    相关资源
    最近更新 更多