首页 > 解决方案 > Powershell Topshelf Uninstall 继续锁定文件

问题描述

使用以下 topshelf 卸载命令:

& $path uninstall -servicename:"MyService" -displayname "MyService"

该命令似乎在它所包含的 powershell 脚本的生命周期内继续运行并锁定文件,因此我无法覆盖它们。有没有办法确保它所做的一切都被终止,以便我的脚本可以不受阻碍地继续进行?

标签: c#powershelldeploymentoctopus-deploytopshelf

解决方案


卸载 Windows 服务的另一种方法。可以用作脚本模块

function Uninstall-WindowsService($serviceName)
{
    $existingService = (Get-WmiObject Win32_Service -filter "name='$serviceName'")
    if ($existingService) 
    {
      Write-Host "Stopping the '$serviceName'."
      Stop-Service $serviceName
      Write-Host "Waiting 3 seconds to allow existing service to stop."
      Start-Sleep -s 3

      $existingService.Delete()
      Write-Host "Waiting 15 seconds to allow service to be uninstalled."
      Start-Sleep -s 15
    }
}

推荐阅读