Powershell Arrays - 检查服务是否已停止

时间:2014-05-06 17:54:14

标签: arrays powershell powershell-v2.0

我知道有更快的方法可以做到这一点,但我不知道如何处理它。我还需要它在PowerShell V2中工作,因为我还需要使用Windows 2003服务器。

基本上这会停止服务,然后检查这些服务是否已停止。我有一个位置,可以在脚本中更早地定义它们。我使用它来将代码部署到服务器,因此根据开发代码,我可能需要停止1到4个服务,并查看是否有一种方法可以定义服务而不必注释下面的代码如果我只使用2个服务而不是4个。

#Turn off Services
    stop-service $Service1 -force
#   stop-service $Service2 -force
#   stop-service $Service3 -force
#   stop-service $Service4 -force

$VerifyServiceStopped1 = Get-Service $Service1 | Where-Object {$_.status -eq "Stopped"} | select -last 1
#   $VerifyServiceStopped2 = Get-Service $Service2 | Where-Object {$_.status -eq "Stopped"} | select -last 1
#   $VerifyServiceStopped3 = Get-Service $Service3 | Where-Object {$_.status -eq "Stopped"} | select -last 1
#   $VerifyServiceStopped4 = Get-Service $Service4 | Where-Object {$_.status -eq "Stopped"} | select -last 1

    if ($VerifyServiceStopped1) {Write-Host $Service1' Stop = Pass (0)'} else {Write-Host $Service1' Stop = Fail (1000)'; Exit '1000'}
#   if ($VerifyServiceStopped2) {Write-Host $Service2' Stop = Pass (0)'} else {Write-Host $Service2' Stop = Fail (1001)'; Exit '1001'}
#   if ($VerifyServiceStopped3) {Write-Host $Service3' Stop = Pass (0)'} else {Write-Host $Service3' Stop = Fail (1002)'; Exit '1002'}
#   if ($VerifyServiceStopped4) {Write-Host $Service4' Stop = Pass (0)'} else {Write-Host $Service4' Stop = Fail (1003)'; Exit '1003'}

有什么建议吗?

由于

德怀特

4 个答案:

答案 0 :(得分:2)

也许是这样的事情?

$services = @(
 'Service1',
 'Service2',
 'Service3',
 'Service4')

 Get-service | 
 Where { $Services -Contains $_.Name} |
 Foreach {
  #Stop Service
  #Verify Service
  #Restart Service
  }

答案 1 :(得分:1)

我不确定你想怎么处理这个,但是因为数组被标记了:

$services = @("ServiceName1","ServiceName2")

#Turn off Services
foreach($service in $services) {
    stop-service $Service1 -force
    $VerifyServiceStopped1 = Get-Service $Service1 | Where-Object {$_.status -eq "Stopped"} | select -last 1
    if ($VerifyServiceStopped1) {
        Write-Host $Service1' Stop = Pass (0)'
    } else {
        Write-Host $Service1' Stop = Fail (1000)'
        Exit '1000'
    }    
}

“所以根据开发代码我可能需要停止1到4个服务”

如果您无法定义该决定所涉及的逻辑,您打算如何自动化它?

答案 2 :(得分:1)

只是想提供整个代码,以防其他人正在寻找类似的东西 - 感谢大家将想法投入到混合中,这非常有帮助。

#Define Services 
     $Service1 = 'servicename'
     $Service2 = 
     $Service3 = 
     $Service4 = 
     $Service5 = 
     $Service6 = 
     $Service7 = 
     $Service8 =
     $Service9 = 

     $services = @(
     $Service1,
     $service2,
     $service3,
     $service4,
     $Service5,
     $Service6,
     $Service7,
     $Service8,
     $Service9)

#Stop Services

     Get-service | 
     Where { $Services -Contains $_.Name} |
     Foreach {

     $_ | stop-service

     } 

     Set-Service |
     Where { $Services -Contains $_.Name} |
     Foreach {

     $_ | -startuptype "Disabled"

     }

#Verify Services

     Get-service | 
     Where { $Services -Contains $_.Name} |
     Foreach {

     if ((Get-Service $_.Name).Status -eq "stopped") {Write-Host 'Service Start Pass (0)'} else {Write-Host 'Start Fail (1000)';Exit '1000'}

     }


#Start Services

     Set-Service |
     Where { $Services -Contains $_.Name} |
     Foreach {

     $_ | -startuptype "Automatic"

     }

     Get-service | 
     Where { $Services -Contains $_.Name} |
     Foreach {

     $_ | start-service

     }


#Verify Services

     Get-service | 
     Where { $Services -Contains $_.Name} |
     Foreach {

     if ((Get-Service $_.Name).Status -eq "running") {Write-Host 'Service Start Pass (0)'} else {Write-Host 'Start Fail (2000)';Exit '2000'}

     }

这允许我让某人列出他们停止/开始部署自定义代码所需的任何服务 - 然后我将移动它以停止可执行文件以替换它们和其他文件以进行验证。遗憾的是,我们将一些exe和dll的集合交给了一些软件部署的热交换,所以这就是为什么需要它。我想要一种方法来定义每个部署需要移入和移出的内容但是不想在整个脚本中注释掉我不需要的行(即只需要两个服务来在整个脚本中注释掉其他内容)。

答案 3 :(得分:0)

直接访问状态:

> (Get-Service -Name "Windows Time").Status -eq "Stopped"
$true

if ((Get-Service -Name "Windows Time").Status -eq "Stopped")
{
    Write-Host "Yep..."
}

或者如果您想要非常简洁,可以使用别名gsv

> (gsv "Windows Time").Status -eq "Stopped"
$true

您也可以创建一个功能:

Function IsStopped ($name)
{
    ((Get-Service $name).Status -eq "Stopped")
}

if (IsStopped "Windows Time")
{
    Write-Host "foo"
}