How do I start multiple services in a specific order and if there is no service available, it will skip to...












0














This script will find the services listed in the variables set. If the service is not running, it will start the service. It also checks and starts the service in a specific order. It also reads the servers from a text file.



However, the question here is how do you make it so that if the service is not available, it will skip to the next available service? Currently, the script just stops when there is no service available.



Function startOTCSServices {

$ComputerPath = "C:PS ScriptsOTCS CSIRTservernames.otcs.txt"
$OTCSServicePath = "KIC-ED-MC02"
$OTCSServicePath1 = "KIC-ED-MC"
$OTCSServicePath2 = "Ascent Capture Service"
$OTCSServicePath3 = "ACIS Remote Synchronization Agent"

try {
$ComputerNames = Get-Content -Path $ComputerPath

if ($ComputerNames -ne $null)
{

foreach ($c in $ComputerNames)
{
Write-Host "Server:" $c -ForegroundColor Yellow
Write-Host "-------------------------------------------"
Write-Host "Starting Services"
Write-Host "-------------------------------------------"

$p = Get-Service -Name $OTCSServicePath -ComputerName $c -ErrorAction SilentlyContinue
$p1 = Get-Service -Name $OTCSServicePath1 -ComputerName $c -ErrorAction SilentlyContinue
$p2 = Get-Service -Name $OTCSServicePath2 -ComputerName $c -ErrorAction SilentlyContinue
$p3 = Get-Service -Name $OTCSServicePath3 -ComputerName $c -ErrorAction SilentlyContinue

while ($p.Status -ne "Running")
{
Start-Service $p -Verbose
Start-Sleep -s 5
$p.Refresh()
if ($p.Status -eq "Running")
{
Write-Host $p "is now started" -ForegroundColor Green
}
}
while ($p1.Status -ne "Running")
{
Start-Service $p1 -Verbose
Start-Sleep -s 5
$p1.Refresh()
if ($p1.Status -eq "Running")
{
Write-Host $p1 "is now started" -ForegroundColor Green
}
}
while ($p2.Status -ne "Running")
{
Start-Service $p2 -Verbose
Start-Sleep -s 5
$p2.Refresh()
if ($p2.Status -eq "Running")
{
Write-Host $p2 "is now started" -ForegroundColor Green
}
}
while ($p3.Status -ne "Running")
{
Start-Service $p3 -Verbose
Start-Sleep -s 5
$p3.Refresh()
if ($p3.Status -eq "Running")
{
Write-Host $p3 "is now started" -ForegroundColor Green
}
}
}
} else { Write-Host "No computers found in file" }
} catch { Write-Host $_ }


}










share|improve this question
























  • Possible duplicate of Powershell script to check if service is started, if not then start it
    – Ken Y-N
    Nov 21 '18 at 2:20










  • instead of feeding in all three services to the same command, use a loop over the list. keep checking for "is it running yet?" OR simply use Start-Sleep for however long you think is adequate.
    – Lee_Dailey
    Nov 21 '18 at 3:15










  • thanks for the replies guys. I've changed my script a little to try to get the specific order. However, is there a way to make it so that if the SERVICE does NOT exist, to skip it and move on to start the NEXT service?
    – wazzie
    Nov 28 '18 at 21:15
















0














This script will find the services listed in the variables set. If the service is not running, it will start the service. It also checks and starts the service in a specific order. It also reads the servers from a text file.



However, the question here is how do you make it so that if the service is not available, it will skip to the next available service? Currently, the script just stops when there is no service available.



Function startOTCSServices {

$ComputerPath = "C:PS ScriptsOTCS CSIRTservernames.otcs.txt"
$OTCSServicePath = "KIC-ED-MC02"
$OTCSServicePath1 = "KIC-ED-MC"
$OTCSServicePath2 = "Ascent Capture Service"
$OTCSServicePath3 = "ACIS Remote Synchronization Agent"

try {
$ComputerNames = Get-Content -Path $ComputerPath

if ($ComputerNames -ne $null)
{

foreach ($c in $ComputerNames)
{
Write-Host "Server:" $c -ForegroundColor Yellow
Write-Host "-------------------------------------------"
Write-Host "Starting Services"
Write-Host "-------------------------------------------"

$p = Get-Service -Name $OTCSServicePath -ComputerName $c -ErrorAction SilentlyContinue
$p1 = Get-Service -Name $OTCSServicePath1 -ComputerName $c -ErrorAction SilentlyContinue
$p2 = Get-Service -Name $OTCSServicePath2 -ComputerName $c -ErrorAction SilentlyContinue
$p3 = Get-Service -Name $OTCSServicePath3 -ComputerName $c -ErrorAction SilentlyContinue

while ($p.Status -ne "Running")
{
Start-Service $p -Verbose
Start-Sleep -s 5
$p.Refresh()
if ($p.Status -eq "Running")
{
Write-Host $p "is now started" -ForegroundColor Green
}
}
while ($p1.Status -ne "Running")
{
Start-Service $p1 -Verbose
Start-Sleep -s 5
$p1.Refresh()
if ($p1.Status -eq "Running")
{
Write-Host $p1 "is now started" -ForegroundColor Green
}
}
while ($p2.Status -ne "Running")
{
Start-Service $p2 -Verbose
Start-Sleep -s 5
$p2.Refresh()
if ($p2.Status -eq "Running")
{
Write-Host $p2 "is now started" -ForegroundColor Green
}
}
while ($p3.Status -ne "Running")
{
Start-Service $p3 -Verbose
Start-Sleep -s 5
$p3.Refresh()
if ($p3.Status -eq "Running")
{
Write-Host $p3 "is now started" -ForegroundColor Green
}
}
}
} else { Write-Host "No computers found in file" }
} catch { Write-Host $_ }


}










share|improve this question
























  • Possible duplicate of Powershell script to check if service is started, if not then start it
    – Ken Y-N
    Nov 21 '18 at 2:20










  • instead of feeding in all three services to the same command, use a loop over the list. keep checking for "is it running yet?" OR simply use Start-Sleep for however long you think is adequate.
    – Lee_Dailey
    Nov 21 '18 at 3:15










  • thanks for the replies guys. I've changed my script a little to try to get the specific order. However, is there a way to make it so that if the SERVICE does NOT exist, to skip it and move on to start the NEXT service?
    – wazzie
    Nov 28 '18 at 21:15














0












0








0







This script will find the services listed in the variables set. If the service is not running, it will start the service. It also checks and starts the service in a specific order. It also reads the servers from a text file.



However, the question here is how do you make it so that if the service is not available, it will skip to the next available service? Currently, the script just stops when there is no service available.



Function startOTCSServices {

$ComputerPath = "C:PS ScriptsOTCS CSIRTservernames.otcs.txt"
$OTCSServicePath = "KIC-ED-MC02"
$OTCSServicePath1 = "KIC-ED-MC"
$OTCSServicePath2 = "Ascent Capture Service"
$OTCSServicePath3 = "ACIS Remote Synchronization Agent"

try {
$ComputerNames = Get-Content -Path $ComputerPath

if ($ComputerNames -ne $null)
{

foreach ($c in $ComputerNames)
{
Write-Host "Server:" $c -ForegroundColor Yellow
Write-Host "-------------------------------------------"
Write-Host "Starting Services"
Write-Host "-------------------------------------------"

$p = Get-Service -Name $OTCSServicePath -ComputerName $c -ErrorAction SilentlyContinue
$p1 = Get-Service -Name $OTCSServicePath1 -ComputerName $c -ErrorAction SilentlyContinue
$p2 = Get-Service -Name $OTCSServicePath2 -ComputerName $c -ErrorAction SilentlyContinue
$p3 = Get-Service -Name $OTCSServicePath3 -ComputerName $c -ErrorAction SilentlyContinue

while ($p.Status -ne "Running")
{
Start-Service $p -Verbose
Start-Sleep -s 5
$p.Refresh()
if ($p.Status -eq "Running")
{
Write-Host $p "is now started" -ForegroundColor Green
}
}
while ($p1.Status -ne "Running")
{
Start-Service $p1 -Verbose
Start-Sleep -s 5
$p1.Refresh()
if ($p1.Status -eq "Running")
{
Write-Host $p1 "is now started" -ForegroundColor Green
}
}
while ($p2.Status -ne "Running")
{
Start-Service $p2 -Verbose
Start-Sleep -s 5
$p2.Refresh()
if ($p2.Status -eq "Running")
{
Write-Host $p2 "is now started" -ForegroundColor Green
}
}
while ($p3.Status -ne "Running")
{
Start-Service $p3 -Verbose
Start-Sleep -s 5
$p3.Refresh()
if ($p3.Status -eq "Running")
{
Write-Host $p3 "is now started" -ForegroundColor Green
}
}
}
} else { Write-Host "No computers found in file" }
} catch { Write-Host $_ }


}










share|improve this question















This script will find the services listed in the variables set. If the service is not running, it will start the service. It also checks and starts the service in a specific order. It also reads the servers from a text file.



However, the question here is how do you make it so that if the service is not available, it will skip to the next available service? Currently, the script just stops when there is no service available.



Function startOTCSServices {

$ComputerPath = "C:PS ScriptsOTCS CSIRTservernames.otcs.txt"
$OTCSServicePath = "KIC-ED-MC02"
$OTCSServicePath1 = "KIC-ED-MC"
$OTCSServicePath2 = "Ascent Capture Service"
$OTCSServicePath3 = "ACIS Remote Synchronization Agent"

try {
$ComputerNames = Get-Content -Path $ComputerPath

if ($ComputerNames -ne $null)
{

foreach ($c in $ComputerNames)
{
Write-Host "Server:" $c -ForegroundColor Yellow
Write-Host "-------------------------------------------"
Write-Host "Starting Services"
Write-Host "-------------------------------------------"

$p = Get-Service -Name $OTCSServicePath -ComputerName $c -ErrorAction SilentlyContinue
$p1 = Get-Service -Name $OTCSServicePath1 -ComputerName $c -ErrorAction SilentlyContinue
$p2 = Get-Service -Name $OTCSServicePath2 -ComputerName $c -ErrorAction SilentlyContinue
$p3 = Get-Service -Name $OTCSServicePath3 -ComputerName $c -ErrorAction SilentlyContinue

while ($p.Status -ne "Running")
{
Start-Service $p -Verbose
Start-Sleep -s 5
$p.Refresh()
if ($p.Status -eq "Running")
{
Write-Host $p "is now started" -ForegroundColor Green
}
}
while ($p1.Status -ne "Running")
{
Start-Service $p1 -Verbose
Start-Sleep -s 5
$p1.Refresh()
if ($p1.Status -eq "Running")
{
Write-Host $p1 "is now started" -ForegroundColor Green
}
}
while ($p2.Status -ne "Running")
{
Start-Service $p2 -Verbose
Start-Sleep -s 5
$p2.Refresh()
if ($p2.Status -eq "Running")
{
Write-Host $p2 "is now started" -ForegroundColor Green
}
}
while ($p3.Status -ne "Running")
{
Start-Service $p3 -Verbose
Start-Sleep -s 5
$p3.Refresh()
if ($p3.Status -eq "Running")
{
Write-Host $p3 "is now started" -ForegroundColor Green
}
}
}
} else { Write-Host "No computers found in file" }
} catch { Write-Host $_ }


}







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 21:24

























asked Nov 21 '18 at 2:11









wazzie

12




12












  • Possible duplicate of Powershell script to check if service is started, if not then start it
    – Ken Y-N
    Nov 21 '18 at 2:20










  • instead of feeding in all three services to the same command, use a loop over the list. keep checking for "is it running yet?" OR simply use Start-Sleep for however long you think is adequate.
    – Lee_Dailey
    Nov 21 '18 at 3:15










  • thanks for the replies guys. I've changed my script a little to try to get the specific order. However, is there a way to make it so that if the SERVICE does NOT exist, to skip it and move on to start the NEXT service?
    – wazzie
    Nov 28 '18 at 21:15


















  • Possible duplicate of Powershell script to check if service is started, if not then start it
    – Ken Y-N
    Nov 21 '18 at 2:20










  • instead of feeding in all three services to the same command, use a loop over the list. keep checking for "is it running yet?" OR simply use Start-Sleep for however long you think is adequate.
    – Lee_Dailey
    Nov 21 '18 at 3:15










  • thanks for the replies guys. I've changed my script a little to try to get the specific order. However, is there a way to make it so that if the SERVICE does NOT exist, to skip it and move on to start the NEXT service?
    – wazzie
    Nov 28 '18 at 21:15
















Possible duplicate of Powershell script to check if service is started, if not then start it
– Ken Y-N
Nov 21 '18 at 2:20




Possible duplicate of Powershell script to check if service is started, if not then start it
– Ken Y-N
Nov 21 '18 at 2:20












instead of feeding in all three services to the same command, use a loop over the list. keep checking for "is it running yet?" OR simply use Start-Sleep for however long you think is adequate.
– Lee_Dailey
Nov 21 '18 at 3:15




instead of feeding in all three services to the same command, use a loop over the list. keep checking for "is it running yet?" OR simply use Start-Sleep for however long you think is adequate.
– Lee_Dailey
Nov 21 '18 at 3:15












thanks for the replies guys. I've changed my script a little to try to get the specific order. However, is there a way to make it so that if the SERVICE does NOT exist, to skip it and move on to start the NEXT service?
– wazzie
Nov 28 '18 at 21:15




thanks for the replies guys. I've changed my script a little to try to get the specific order. However, is there a way to make it so that if the SERVICE does NOT exist, to skip it and move on to start the NEXT service?
– wazzie
Nov 28 '18 at 21:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404386%2fhow-do-i-start-multiple-services-in-a-specific-order-and-if-there-is-no-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404386%2fhow-do-i-start-multiple-services-in-a-specific-order-and-if-there-is-no-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga