【问题标题】:Powershell Topshelf Uninstall continues to lock filesPowershell Topshelf Uninstall 继续锁定文件
【发布时间】:2018-10-31 14:06:54
【问题描述】:
使用以下 topshelf 卸载命令:
& $path uninstall -servicename:"MyService" -displayname "MyService"
该命令似乎在它所包含的 powershell 脚本的生命周期内继续运行并锁定文件,因此我无法覆盖它们。有没有办法确保它所做的一切都被终止,以便我的脚本可以不受阻碍地继续进行?
【问题讨论】:
标签:
c#
powershell
deployment
octopus-deploy
topshelf
【解决方案1】:
卸载 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】:
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"}