首页 > 解决方案 > 为什么要更改所有服务而不只是 if 条件服务?

问题描述

我正在编写一个脚本,它将传入两个变量和一个 csv 文件,以更改 csv 文件中列出的计算机上的服务。

该命令将采用以下格式:例如。start SQLService或者stop PBIService

给定 csv 文件:

服务器,服务

server1,SQLService

server2,PBI服务

server3,PBI服务

param($task, $service) #arguments from cmd line input

if($task -eq "start")
{
    Set-Variable -Name "task" -Value "running"
}
elseif($task -eq "stop")
{
    Set-Variable -Name "task" -Value "stopped"
}

if($service -eq "SQLService")
{
    Set-Variable -Name "SQLsvc" -Value "SQLService"
}
elseif($service -eq "PBIService")
{
    Set-Variable -Name "PBIsvc" -Value "PBIService"
}

Import-CSV .\csvfile.csv |
    ForEach {
        if($_.service -eq "SQLService")
        {
            $getService = Get-Service $SQLsvc -ComputerName $_.Server
            $oldstatus = $getService.status
            $getService | 
            Set-Service -Status $task -PassThru |
                Select MachineName, Name, Status, 
                    @{n='OldStatus';e={$oldStatus}}
        }
        elseif($_.Service -eq "PBIService")
        {
            $getService = Get-Service $PBIsvc -ComputerName $_.Server
            $oldstatus = $getService.status
            $getService | 
            Set-Service -Status $task -PassThru |
                Select MachineName, Name, Status, 
                    @{n='OldStatus';e={$oldStatus}}
        }
    } |
    tee output.txt

但是,当我出于某种原因运行它时,它会影响所有服务......

MachineName                    Name                    Status OldStatus
-----------                    ----                    ------ ---------
server1                         SQLService          Running   Running
server2                         PBIService          Running   Running
Set-Service : Service 'Microsoft Monitoring Agent Audit Forwarding (AdtAgent)' cannot be started due to the following
error: Cannot start service AdtAgent on computer 'server3'.
+             Set-Service -Status $task -PassThru |
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], Se
   rviceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.SetServiceCommand

server3 AdtAgent               Stopped ...ed...}
Set-Service : Service 'AllJoyn Router Service (AJRouter)' cannot be started due to the following error: Cannot start
service AJRouter on computer 'server3'.
+             Set-Service -Status $task -PassThru |
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], Se
   rviceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.SetServiceCommand

server3 AJRouter               Stopped ...ed...}

另外,为什么如果我命令start SQLService, PBIService 也会启动?由于某种原因,条件无法正常工作......在这种情况下,唯一受影响的服务器和服务应该是 server1,因为根据 if 条件,该服务器具有service = SQLService

标签: powershell

解决方案


Inside the foreach you don't check what was passed as an argument.

I suggest to only process lines from the csv which match the passed $Service

Import-CSV .\csvfile.csv | Where-Object Service -eq $Service |

Also the script blocks in the if/elseif are identical aside from $SQLsvc/$PBIsvc which BOTH stem from passed $service, so could be merged and directly use $service


推荐阅读