代码之家  ›  专栏  ›  技术社区  ›  gmn

powershell topshelf卸载继续锁定文件

  •  0
  • gmn  · 技术社区  · 7 年前

    使用以下topshelf uninstall命令:

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

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Alex M    7 年前

    卸载Windows服务的另一种方法。可以用作 script module .

    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
        }
    }
    
        2
  •  0
  •   user10494751    6 年前
    If (Get-Service $serviceName -ErrorAction SilentlyContinue) {
        $servicePath = (Get-WmiObject win32_service | ?{$_.Name -like $serviceName} | Select-Object Name, @{Name="Path"; Expression={$_.PathName.split('"')[1]}}).Path
        Write-Host "Uninstalling service from $servicePath"
        Invoke-Expression "$servicePath uninstall"}
    
    推荐文章