除了我的问题之外,如果可能的话,我希望对特殊的 Windows 服务应用程序有一些了解,以防我无法正确理解这一切是如何工作的。谢谢你。
背景:
我正在编写一个 PowerShell 脚本,该脚本手动创建 Windows 服务,并删除或清理它(如果存在)。
问题:
当我在 PowerShell 脚本中创建和删除服务时,它们可能会被打开的服务应用程序阻止
我认为潜在的解决方案可能是
如有任何帮助,我们将不胜感激!
我想以此作为序言,说我可能会将我的结果与 sc.exe 创建和删除混淆,但无论如何我相信我在这里遇到了问题。
到目前为止,我已决定尝试检查服务应用程序是否打开,但未能使其正常工作。根据我创建和删除 Windows 服务的经验,我让这个应用程序阻止了在 PowerShell 中运行这些命令的能力。一个例子是(sc.exe删除“SomeService”)提供反馈,例如“x被标记为删除”,但不会删除它,直到它关闭或服务本身停止。
我也尝试过终止服务的任务或进程,但不确定我是否可以做到这一点。我无法绕过它的权限。
正如评论中提到的,这是一个可以为您工作的片段
# Set the name of the service and process to check for
$serviceName = "Application1Service"
$processName = "application1.exe"
# Check if the service is running
if (Get-Service $serviceName -ErrorAction SilentlyContinue | Where-Object {$_.Status -eq "Running"}) {
# Stop the service
Stop-Service $serviceName
# Check if the process is running
if (Get-Process $processName -ErrorAction SilentlyContinue) {
# Kill the process
Stop-Process -Name $processName -Force
}
# Delete the service (CmdLet available from Powershell 6.0)
Remove-Service $serviceName
}