powershell - 为什么要更改所有服务而不只是 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
解决方案
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
推荐阅读
- wordpress - 根据复选框或选择创建第二个条目 - 重力形式
- amazon-web-services - 将 EC2 t2.medium 迁移到 t2.large ssh 问题
- python - UnboundLocalError:赋值前引用的局部变量()
- flutter - my-json-server.typicode.com 服务是否允许您免费创建自己的 api?
- python - 如果从 Pyinstaller 编译的应用程序调用,可执行文件找不到库
- c++ - 如何在 Eclipse 中从我的项目中包含 .cpp 和 .h 文件
- c# - 将布尔值输出到控制台
- excel - 我真的需要选择代码才能运行吗?
- r - 如何使用R中的igraph从有向子树中获取所有叶节点?
- c - C修剪带有多个单词的字符串