Powershell - Start ordered sequence of services - windows

I need to start an ordered sequence of services, i need that each service will be up and running before try to start the next one, how can I achieve this in Powershell? How can I wait for the stop too?
Thanks,
DD

Don't do this manually (regardless of scripting language). Define proper dependencies between the services and Windows will start/stop them in the correct order. You can use the sc utility to define the dependencies:
sc config Svc2 depend= Svc1
If a service should depend on more than one other service you separate the dependent services with forward slashes:
sc config Svc5 depend= Svc3/Svc4
Note that the = must be followed by a space and must not be preceded by one.

If you have a list of service names (say in an array), then foreach service:
Get its status
If not running, then start it
With a delay in the loop, check its status until it is running
The key is likely to be handling all the possibilities for #3 including the service failing.
But an outline would be something like (without handling the error cases):
$serviceNames | Foreach-Object -Process {
$svc = Get-Service -Name $_
if ($svc.Status -ne 'Running') {
$svc.Start()
while ($svc.Status -ne 'Running') {
Write-Output "Waiting for $($svc.Name) to start, current status: $($svc.Status)"
Start-Sleep -seconds 5
}
}
Write-Output "$($svc.Name) is running"
}
Get-Service returns an instance of System.ServiceProcess.ServiceController which is "live"—indicating the current state of the service, not just the state when the instance was created.
A similar stop process would replace "Running" with "Stopped" and the "Start" call with "Stop". And, presumably, reverse the order of the list of services.

To stop Services
$ServiceNames = Get-Service | where {($_.Name -like "YourServiceNameHere*")-and ($_.Status -eq "Running")}
Foreach-Object {$_.(Stop-Service $serviceNames)}
To start Services
$ServiceNames = Get-Service | where {($_.Name -like "YourServiceNameHere*")-and ($_.Status -ne "Running")}
Foreach-Object {$_.(Start-Service $ServiceNames)}

To start and wait in order
[array]$Services = 'svc1', 'svc2', 'svc3'
$Services | foreach {
start-service $_
(get-service $_).WaitForStatus('Running')
}
The wait for status might not be needed. I have no system that have slow services to test with.
To stop and wait in order
[array]$Services = 'svc1', 'svc2', 'svc3'
[array]::Reverse($Services)
$Services | stop-service

Related

Logging off from all servers in a domain

Occasionally I forget to log off from a server or am disconnected through an error and I don't remember the name of the server. And my domain account starts getting periodically locked out, so I have to access logs on DC to find out which server(s) keep locking my account and log off from it/them. So I wanted to write to script in powershell that would log me off from all servers in a domain (with the exception of the server where I run the script on of course) without me needing to search which to log off from. This is what I have:
$ErrorActionPreference = "Silentlycontinue"
$Servers = (Get-ADComputer -Filter *).Name
$ScriptBlock = {
$Sessions = quser | ?{$_ -match $env:USERNAME}
if (($Sessions).Count -ge 1)
{
$SessionIDs = ($Sessions -split ' +')[2]
Write-Host "Found $(($SessionIDs).Count) user login(s) on $Server."
$SessionIDs | ForEach-Object
{
Write-Host "Logging off session [$($_)]..."
logoff $_
}
}
}
foreach ($Server in $Servers)
{
if ($Server -isnot $env:COMPUTERNAME)
{
Invoke-Command -ComputerName $Server -ScriptBlock {$ScriptBlock}
}
}
But when I launch the script, nothing happens. The script doesn't return any errors but doesn't log me off from any server, nor does it write any of the messages from Write-Host cmdlet, obviously. I noticed the $SessionIDs variable definition only returns ID of the first session. Usually this shouldn't be a problem, since it's unlikely I will have more than one session on a server, but I'd like to have this insurance. Can anyone tell me what's wrong in the script?
I notice a few things...
"First, I don't think quser | Where-Object {$_ -match $env:USERNAME} will ever return anything. The output of quser will not contain the hostname."
Try this for getting logon sessions:
$Sessions = (query user /server:$Env:ComputerName) -split "\n" -replace '\s\s+', ';' |
ConvertFrom-Csv -Delimiter ';'
Next, when you reference the $Server variable on the remote machine in your script block, it is out of scope. You would need to use $Using:Server in the script block.
Lastly, the -isnot operator doesn't compare value, it compares type. So in your last foreach, the if statement evaluates to "if type string is not type string" and will not run. Try -ne or -notlike instead.
Working with objects is much easier if you can just parse the output of QUser.exe. Given your scenario, here's my take on it:
$servers = (Get-ADComputer -Filter '*').Name.Where{$_ -ne $env:COMPUTERNAME}
foreach ($server in $servers)
{
if (-not ($quser = ((QUser.exe /server:$server) -replace '\s{20,39}',',,' -replace '\s{2,}',',' 2>&1) | Where-Object -FilterScript { $_ -match $env:USERNAME })) {
Continue
}
Write-Verbose -Message "$($quser.Count) session(s) found on $server." -Verbose
($quser.Trim() | ConvertFrom-Csv -Header 'USERNAME','SESSIONNAME','ID','STATE','IDLE TIME','LOGON TIME').foreach{
Write-Verbose -Message "Logging user [$($_.UserName)] off." -Verbose
LogOff.exe $_.ID /server:$server
}
}
Filtering should always happen before hand meaning, filter out your computer name on your first call to Get-ADComputer. Since you're using QUser.exe and LogOff.exe to begin with, I'd recommend the use of it all the way through since LogOff accepts an ID value that QUser outputs.
Next, placing the call to quser inside your if statement does two things in this case.
Filters for all users matching $ENV:UserName
Returns $true if anything is found, and $false if not found.
So, switching the results using -not will turn $false into $true allowing the execution of the code block which will just continue to the next server.
This in turn doesn't bother with the rest of the code and continues onto the next computer if no matching names were found.
The use of $quser inside the if statement is so you can save the results to it if more than one name is found; (..) allows this as it turns the variable assignment into an expression having the output pass through onto the pipeline where it is either empty, or not.
Finally, referencing the $quser variable we can convert the strings into objects piping to ConvertFrom-Csv. Only step left to do is iterate through each row and passing it over to LogOff to perform the actual logoff.
If you've noticed, the headers are manually-specified because it is filtered out by the Where-Object cmdlet. This is a better approach seeing as there could be "more than one" RDP Session, now you're just left with those sessions matching the name which can be saved to $quser, so no extra filtering is needed down the line.
So I modified the script this way and it works, sort of. It logs off account from servers, which is the main goal. There are still some glitches, like the message it sends from the first Write-Host doesn't give server's name, the message from second one gives a different value than it should (it gives [1] value after -split instead of [2] for some reason; but those are not really that important things, even though I will try to make at least the first message right) and $SessionIDs still gives only the first value, but usually you shouldn't have more than one RDP session per server. I've seen more sessions of one user, but that is very rare. But I'd also like to fix this if possible. Nevertheless, the script basically does the most important thing. But if someone has a suggestion how to fix the glitches I mentioned I would be grateful.
$ErrorActionPreference = "Silentlycontinue"
$Servers = (Get-ADComputer -Filter *).Name
$ScriptBlock = {
$Sessions = quser | ?{$_ -match $env:USERNAME}
if (($Sessions).Count -ge 1)
{
$SessionIDs = , ($Sessions -split ' +')[2]
Write-Host "Found $(($SessionIDs).Count) user login(s) on $Server."
Foreach ($SessionID in $SessionIDs)
{
Write-Host "Logging off session $SessionID..."
logoff $SessionID
}
}
}
foreach ($Server in $Servers)
{
if ($Server -ne $env:COMPUTERNAME)
{
Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock
}
}

Kill the process if start time is less than 2 hours

I need to kill the process if start time is less than 2 hours.
I have written the below cmdlet to find out the starttime but how to find out if is it less than 2 hours:
get-process process1 | select starttime
There is also a possibility that on some hosts process1 is not running. so I need to check first if the process1 is running
You can use a loop of your choice, in this example ForEach-Object in addition to an if condition to check if the StartTime value is lower than 2 hours.
If you need to check first is the process is running then you would need to get all processes and filter by the process name you're looking for. Then check if the returned value from Where-Object is $null or not.
$procName = 'myprocess'
$process = Get-Process | Where-Object Name -EQ $procName
if(-not $process) {
Write-Warning "$procName not found!"
}
else {
$process | ForEach-Object {
if($_.StartTime -lt [datetime]::Now.AddHours(-2)) {
try {
'Attempting to stop {0}' -f $_.Name
Stop-Process $_ -Force
'{0} successfully stopped.' -f $_.Name
}
catch {
Write-Warning $_.Exception.Message
}
}
}
}

How to find the process ID of a running scheduled task?

I can determine running tasks with:
$TaskService = new-object -ComObject('Schedule.Service')
$TaskService.connect()
$TaskFolder = $TaskService.GetFolder('\')
$TaskFolder.gettasks(1) | ? {$_.state -eq 4}
Is there any way to identify the process IDs of those tasks if they start the same program (i.e. process name) as other existing processes?
My goal is a PowerShell script started from a scheduled task that can identify which scheduled task it is running under. I can easily determine the PoSh processID with $PID, but I don't know how to link that to a particular scheduled task.
Thanks.
This should work if you have this running in the script that is fired as an action. It will get the task path assuming it can be found by the RunningTasks COM object method
# Initiate a COM object and connect
$TaskService = New-Object -ComObject('Schedule.Service')
$TaskService.Connect()
# Query for currently running tasks
# 0 - the user is permitted to see.
# 1 - 0 + Hidden
$runningTasks = $TaskService.GetRunningTasks(0)
# Get the task associated to a certain PID
$runningTasks | Where-Object{$_.EnginePID -eq $PID} | Select-Object -ExpandProperty Path
Credit goes to eryksun for pointing out the method and linking to the ITaskService interface on MSDN
There is something to be said about the other suggestion of just telling your script what is running from via an extra parameter. That way you don't have to worry about a COM dependency.
param(
[string]$SuperImportantString,
[int]$NumberofBagels,
[string]$TaskInitiated
)
Set-Content -Path $file -Value "I'm running from $TaskInitiated"
Yes, this does make it more manual but you would have ultimate control over the text and such used and not have to worry about multiple tasks running from the same PID.
This seems like it tells me exactly which task started the script:
$EventFilter = #{
Logname = 'Microsoft-Windows-TaskScheduler/Operational'
ProviderName = "Microsoft-Windows-TaskScheduler"
Id = 129
Data = "$PID"
}
$ThisProcessEvent = Get-WinEvent -FilterHashtable $EventFilter -MaxEvents 1 -ErrorAction SilentlyContinue
$EventXML = [xml]$ThisProcessEvent.toxml()
$TaskFullName = $eventxml.event.eventdata.data | ? {$_.name -eq 'taskname'} |select -ExpandProperty "#text"

Stop services, run batch files, then start services

I've to stop multiples services, check the services if they are correctly stopped THEN run multiple batch files. When the batch jobs are done, start again the stopped services at the first place. I started the following script but could not go further.
#Define Services
$service1 = 'StiSvc'
$service2 = 'AdobeARMservice'
$services = #(
$service1,
$service2
)
#Stop Services
Get-Service |
Where { $services -contains $_.Name } |
Foreach {
$_ | Stop-Service
}
#Verify Services
Get-Service |
Where { $services -contains $_.Name } |
Foreach {
if ((Get-Service $_.Name).Status -eq "stopped") {
Write-Host 'Service Stop Pass (0)'
} else {
Write-Host 'Service Stop Failed (1000)';
exit '1000'
}
}
#Start batch
if ($services.Status -eq "Stopped") {
Start-Process "cmd.exe" "/c C:\download\hello.bat"
} else {
Start-Sleep -s 10
}
The following should work:
$services = 'StiSvc', 'AdobeARMservice'
Stop-Service $services -ErrorAction Stop
& 'C:\download\hello.bat'
& 'C:\path\to\other.cmd'
#...
Start-Service $services
If it doesn't, you need to provide more information about what exactly fails, and how. Include all error and status messages.
Thank you all,
I modified the script as follow. The script stops 2 services, when the 2 services are stopped it start the batch file.
#Stop Services
Get-Service StiSvc, AdobeARMservice | Stop-Service
#Verify Services
if (((Get-Service StiSvc).Status -eq "stopped") -and
((Get-Service AdobeARMservice).Status -eq "stopped"))
# Start batch
{Start-Process "cmd.exe" "/c C:\download\hello.bat"}
I’m working on to improve the script. I’ll come back to you asap.
Regards

Powershell Automation for Services

I have 100+ servers for which I have to check services. On almost 5 servers we have identical services. Each boxes contains min of 3 services.
I am importing content from the file I saved in a location. For Server name I am good. For Service I have saved like service_starting_name* in column under the file like below
AA*
BB*
CC*
Below is the code. Is this good idea for automation as per below code ?
$ServerName = Get-Content "Absolutepath"
$Service = Get-Content "Absolutepath"
foreach ($Server in $ServerName) {
write-host $($server)
Get-Service -ComputerName $Server $Service
}
Also , How can we do a better display like, without printing the service name ?
Suppose, In Server X , 5 services , so if all services are running just print all good on that server.
I tried using if conditions but as there are many services , it is printing multiple times because for for each loop .
Please suggest.
You can play around with various things..
For testing I use localhost, when you try:
Get-Service -ComputerName localhost -DisplayName *sophos*
You get:
(Get-Service -ComputerName localhost -DisplayName *sophos*).count
result 10
(Get-Service -ComputerName localhost -DisplayName *sophos*).Status -contains "stopped"
result True
So if you use:
if (!((Get-Service -ComputerName localhost -DisplayName *sophos*).Status -contains stopped)) { Write-Host "Localhost: All ok" }
Or:
if (!(Get-Service -ComputerName $server -DisplayName $Service | select status | where {$_.Status -like "Stopped"})) { Write-Host "$($Server): All OK" }
You can use various checks for services "stopping" or whatever..
The above are just to give you ideas!
Hope it helps.

Resources