使用以下topshelf uninstall命令:
& $path uninstall -servicename:"MyService" -displayname "MyService"
该命令似乎继续运行,因为它包含在powershell脚本的生命周期中并锁定文件,因此我无法覆盖它们。有没有办法确保它的作用终止,所以我的脚本可以不受阻碍地进行?
卸载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
}
}
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"}