by rcjay272 at 2013-02-23 18:19:12
All,by mjolinor at 2013-02-23 19:26:00
I have the following code I am having trouble with.
I would like it to check to see if a service call "AVSMPROXYSVC" is running and if not start the process. I would also like to write if the service is running or service is not running for each server in a list.
I need some help to get this all straight.
Thank you for your help,
Rob
$servers = Get-Content -Path C:\Load_Stuff\MonitoringScripts\Env2_TS_Servers.txt
ForEach ($server in $servers) {Get-service -ComputerName $server -Name avsmproxysvc }
if ($check -eq $null) {
Write-Host "Process is not running on $server"
Start-Service $server -Name avsmproxysvc
}
else {
Write-Host "Process is running"
}
CURRENT OUTPUT
PS C:\Load_Stuff\MonitoringScripts\PowerShell> .\CheckAVSMProxyEXEup2.ps1
Status Name DisplayName
------ ---- -----------
Running avsm AVSM Proxy Service
Stopped avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Stopped avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Stopped avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Running avsm AVSM Proxy Service
Process is not running on ts18
Start-Service : A positional parameter cannot be found that accepts argument ‘ts18’.
At C:\Load_Stuff\MonitoringScripts\PowerShell\CheckAVSMProxyEXEup2.ps1:5 char:14
+ Start-Service <<<< $server -Name avsmproxysvc
+ CategoryInfo : InvalidArgument: ([Start-Service], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartServiceCommand
OUTPUT desired
Process is not running on ts17 - restarted
Process is not running on ts18 - restarted
Process is not running on ts19 - restarted
Process is running on ts20
Process is running on ts21
Process is running on ts22
While get-service will work on remote systems (and accepts a -ComputerName parameter to specify which one), the other *Service cmdlets only work on the local system.by rcjay272 at 2013-02-23 20:57:44
To control services on remote systems, you can use invoke-command to run the commands on the remote system if you have remoting enabled, or use WMI (win32_service), or the start() and stop() methods of the service objects returned by get-service.
[quote="mjolinor"]While get-service will work on remote systems (and accepts a -ComputerName parameter to specify which one), the other *Service cmdlets only work on the local system.by DexterPOSH at 2013-02-23 21:53:10
To control services on remote systems, you can use invoke-command to run the commands on the remote system if you have remoting enabled, or use WMI (win32_service), or the start() and stop() methods of the service objects returned by get-service.[/quote]
mjolinor,
Thanks for the help…again.
If I understand you correct…could I use something like this to call and start the service?
(gwmi -computername $server -class win32_service | Where-Object { $.Name
-eq “$AVSM†}).startservice()
If so…could I just drop it into the script as such?$servers = Get-Content -Path C:\Load_Stuff\MonitoringScripts\Env2_TS_Servers.txt
ForEach ($server in $servers) {Get-service -ComputerName $server -Name avsmproxysvc }
if ($check -eq $null) {
Write-Host "Process is not running on $server"
(gwmi -computername $server -class win32_service | Where-Object { $.Name
-eq “$avsmproxysvc †}).startservice()
}
else {
Write-Host "Process is running"
}
I think WMI methods can be used here but with PowerShell v2 we can always use the Set-Service cmdlet to start,stop,pause a service and even change the startup type of a service on a remote system.by mjolinor at 2013-02-24 08:40:07
An Example# Ensures that the Service is started
Get-Service -Name bits -ComputerName localhost | Set-Service -Status Running -PassThru
You can try this and let the community know if it worked for you
Regads,
Dexter
I’d start with something more like this:
foreach ($server in $serverList)
{
$services = @(Get-Service -ComputerName $server -Name $serviceName)
foreach ($Service in $Services)
{
switch ($service.status)
{
'Running' {"Service {0} is running on {1}" -f $Service.Name,$server}
'Stopped' {
"Service {0} is not running on {1} - restarted" -f $Service.Name,$server
$Service.Start()
}
'Paused' {"Service {0} is paused on {1}" -f $Service.Name,$server}
'StartPending' {"Service {0} is pending Start on {1}" -f $Service.Name,$server}
'StopPending' {"Service {0} is pending Stop on {1}" -f $Service.Name,$server}
'PausePending' {"Service {0} is pending Pause on {1}" -f $Service.Name,$server}
'ContinuePending' {"Service {0} is pending Continue on {1}" -f $Service.Name,$server}
Default {"Unrecognized status {0} for {1} on {2}" -f $Service.Status,$Service.Name,$Server}
}
}
}
Then you have the option of using wildards in the service name, and it will deal with multiple services being returned by get-service, and you can easily add code to the switch script blocks to handle other possible states the service may be in other than "Running" or "Stopped".