简体   繁体   English

在 Powershell 中停止服务时是否有绕过依赖服务的功能?

[英]Is There a function to bypass dependent services when stoping a service in Powershell?

I have some issues with my Powershell code我的 Powershell 代码有一些问题

I need to stop and disable some services with this script BUT there are some issues, here it is:我需要使用此脚本停止并禁用某些服务,但存在一些问题,这里是:

Get-Content -path $PWD\servicestop1.txt | ForEach-Object 
{
    $service = $_

    (Set-Service -Name $service -Status Stopped -StartupType Disabled -PassThru )
}

1 - I encounter some dependencies issue when I want to stop some services 1 - 当我想停止某些服务时遇到一些依赖问题

2 - I can't get to disable and stop them in the same script 2 - 我无法在同一个脚本中禁用和停止它们

Do you have any idea?你有什么主意吗? Thanks a lot!非常感谢!

PS: I tried to use the parameter "-force" but it didn't work PS:我尝试使用参数“-force”但没有用

Without knowing the servicestop1.txt actually holds valid service names, You may try this:在不知道servicestop1.txt实际上包含有效服务名称的情况下,您可以尝试以下操作:

Get-Content -Path "$PWD\servicestop1.txt" | ForEach-Object {
    $service = Get-Service -Name $_ -ErrorAction SilentlyContinue
    if ($service) {
        if ($service.Status -eq 'Running') {
            # stop the service and wait for it
            $service | Stop-Service -Force
        }
        if ($service.StartType -ne 'Disabled') {
            $service | Set-Service -StartupType Disabled
        }
    }
    else {
        Write-Warning "Could not find service with name '$_'"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM