Вопрос

I know there is a quicker way of doing this but I just don't know how to approach it. I also need it to work in PowerShell V2 as I still have Windows 2003 servers I need to handle with this.

Basically this would stop services and then check to see if those services were stopped. I have a location to define them earlier in the script. I am using this to deploy code to servers so depending on the code from Development I may need to stop 1 to 4 services and looking to see if there is a way that I can define the services and not have to comment out code below if I only use 2 services as opposed to four.

#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'}

Any suggestions?

Thanks

Dwight

Это было полезно?

Решение 3

Just wanted to provide the entire code used in case someone else is looking for something similar - thanks to everyone for throwing ideas into the mix it was very helpful.

#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'}

     }

This allows me to have someone list any services they need to stop / start to deploy custom code out there - also then I will be moving this to stop executables in order to replace them and other files for verification. Sadly we are handed a collection of exe's and dll's to just hot swap for some deployments of software so this is why this was needed. I wanted a way of defining what I need to move in and out per deployment but didn't want to comment out lines throughout the script that I didn't need (i.e. only had two services so needed to comment out the others throughout the script).

Другие советы

Something like this, perhaps?

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

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

I'm not sure how you want to handle this, but since arrays is tagged:

$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'
    }    
}

"so depending on the code from Development I may need to stop 1 to 4 services"

If you cannot define the logic involved in that decision, how do you plan to automate it?

Just access the status directly:

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

or

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

or if you want to be really terse, you can use the alias gsv:

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

You could also make a function:

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

if (IsStopped "Windows Time")
{
    Write-Host "foo"
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top