INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Script to delete Services not running

Script to delete Services not running

(OP)
I currently have a powershell script that searches all my computers that are listed in a text document for a service that is listed in another text document.
and then it displays if the service is running or stopped or not installed.

What I need to do is set it up so that if the service is stopped the script will delete the service from the computer.
Manually I can do this with SC \\servername delete "servicename" then problem is I have over 5000 computers and I do not want to do this for every machine.
The other issue is I just can not enter in the servicename as it is different on each machine since the program that installs the service is setup so after the name it adds a timestamp to the name.

So in a nutshell I need the script to basically scan each computer in an txt document 1 for "servicename*" in txt document 2 if it returns stopped delete

RE: Script to delete Services not running

What do you have so far?


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

(OP)
############################Define Server & Services Variable ###############

$serverList = Get-Content ".\server.txt"
$servicesList = Get-Content ".\services.txt"

#############################Define other variables##########################

$report = ".\report.csv"

$smtphost = "mail.domainname.com"
$from = "myself@domainname.com"
$to = "myself@domainname.com"

##############################################################################

$checkrep = Test-Path ".\report.csv"

If ($checkrep -like "True")

{

Remove-Item ".\report.csv"


}

New-Item ".\report.csv" -type file

################################ADD HTML Content#############################

Add-Content $report "<html>"
Add-Content $report "<head>"
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $report '<title>Service Status Report</title>'
add-content $report '<STYLE TYPE="text/css">'
add-content $report "<!--"
add-content $report "td {"
add-content $report "font-family: Tahoma;"
add-content $report "font-size: 11px;"
add-content $report "border-top: 1px solid #999999;"
add-content $report "border-right: 1px solid #999999;"
add-content $report "border-bottom: 1px solid #999999;"
add-content $report "border-left: 1px solid #999999;"
add-content $report "padding-top: 0px;"
add-content $report "padding-right: 0px;"
add-content $report "padding-bottom: 0px;"
add-content $report "padding-left: 0px;"
add-content $report "}"
add-content $report "body {"
add-content $report "margin-left: 5px;"
add-content $report "margin-top: 5px;"
add-content $report "margin-right: 0px;"
add-content $report "margin-bottom: 10px;"
add-content $report ""
add-content $report "table {"
add-content $report "border: thin solid #000000;"
add-content $report "}"
add-content $report "-->"
add-content $report "</style>"
Add-Content $report "</head>"
Add-Content $report "<body>"
add-content $report "<table width='100%'>"
add-content $report "<tr bgcolor='Lavender'>"
add-content $report "<td colspan='7' height='25' align='center'>"
add-content $report "<font face='tahoma' color='#003399' size='4'><strong>Service Status Report</strong></font>"
add-content $report "</td>"
add-content $report "</tr>"
add-content $report "</table>"

add-content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='10%' align='center'><B>Server Name</B></td>"
Add-Content $report "<td width='50%' align='center'><B>Service Name</B></td>"
Add-Content $report "<td width='10%' align='center'><B>Status</B></td>"
Add-Content $report "</tr>"

################################## Get Services Status #################################################

Function servicestatus ($serverlist, $serviceslist)

{

foreach ($machineName in $serverlist)

{
foreach ($service in $serviceslist)
{

$serviceStatus = get-service -ComputerName $machineName -Name $service

if ($serviceStatus.status -eq "Running") {

Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $machineName</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$svcName</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
Add-Content $report "</tr>"

}

else
{
Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"
Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
Add-Content $report "</tr>"

}

}
}

}
############################################Call Function#############################################

servicestatus $serverList $servicesList

############################################Close HTMl Tables#########################################

Add-content $report "</table>"
Add-Content $report "</body>"
Add-Content $report "</html>"

#############################################Send Email##############################################

$subject = "Daily Service Monitor"
$body = Get-Content ".\report.csv"
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)

#####################################################################################################

RE: Script to delete Services not running

I don't see where you're trying to remove the service.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

(OP)
That is the point where I am not for sure how to do it.

Right now I am just using a command prompt and typing in SC \\servername delete "servicesname"

I am asking for help to try and incorporate it into the powershell script based on the criteria in the original post.

RE: Script to delete Services not running

I haven't tested it, but this might get you closer

CODE -->

Function servicestatus ($serverlist, $serviceslist)
{
 foreach ($machineName in $serverlist)
	{
	 foreach ($service in $serviceslist)
		{
		 $serviceStatus = get-service -ComputerName $machineName -Name $service

		 Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Red
		 $svcName = $serviceStatus.name
		 $svcState = $serviceStatus.status
		 Add-Content $report "<tr>"
		 
		 if ($serviceStatus.status -eq "Running")
			{
			 Add-Content $report "<td bgcolor= 'GainsBoro' align=center><B>$machineName</B></td>"
			 Add-Content $report "<td bgcolor= 'GainsBoro' align=center><B>$svcName</B></td>"
			 Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
			}
		 else
			{
			 Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"
			 Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"
			 Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
			 sc.exe \\$machineName config $service start= disabled
			 sc.exe \\$machineName delete $service
			}

		 Add-Content $report "</tr>"
		}
	}
} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

(OP)
Will that work if $service has a wildcard. so lets say the service is TEST1 and the next service is TEST2 and so forth. I want to say any TEST* that is not running delete.
Remember the service name is different on every computer only the first few characters are the same.

RE: Script to delete Services not running

No. If that's the case you'd need to check for the services, first, putting them into an array. Then go through the array checking each service as to whether or not it's running.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

Again, I haven't tested it but you would need to do something like this.

CODE -->

Function servicestatus ($serverlist, $serviceslist)
{
 foreach ($machineName in $serverlist)
	{
	 foreach ($service in $serviceslist)
		{
		 $found_services = (Get-Service | Where-Object {$_.name -like $service}).name
		 foreach ($found in $found_service)
			{
			 $serviceStatus = get-service -ComputerName $machineName -Name $found
			 
			 $svcName = $serviceStatus.name
			 $svcState = $serviceStatus.status
			 Write-Host $machineName `t $svcName `t $svcState -ForegroundColor Red
			 Add-Content $report "<tr>"
			 
			 if ($serviceStatus.status -eq "Running")
				{
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center><B>$machineName</B></td>"
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center><B>$svcName</B></td>"
				 Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
				}
			 else
				{
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"
				 Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
				 sc.exe \\$machineName config $found start= disabled
				 sc.exe \\$machineName delete $found
				}
			}

		 Add-Content $report "</tr>"
		}
	}
} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

(OP)
With the below code now it no longer scans the computers or atleast I should say I now get a blank report. When I check the computer it still has the extra services so it is not deleting either.

Function servicestatus ($serverlist, $serviceslist)

{

foreach ($machineName in $serverlist)

{
foreach ($service in $serviceslist)
{
$found_services = (Get-Service | Where-Object {$_.name -like $service}).name
foreach ($found in $found_service)
{

$serviceStatus = get-service -ComputerName $machineName -Name $service

if ($serviceStatus.status -eq "Running") {

Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $machineName</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$svcName</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
Add-Content $report "</tr>"

}

else
{
Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"
Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
Add-Content $report "</tr>"

sc.exe \\$machineName delete $found
}

}

}


}

}

RE: Script to delete Services not running

Found a typo. Try this:

CODE -->

Function servicestatus ($serverlist, $serviceslist)
{
 $found_service = @()
 foreach ($machineName in $serverlist)
	{
	 foreach ($service in $serviceslist)
		{
		 $found_services = (Get-Service -ComputerName $machineName | Where-Object {$_.name -like $service}).name
		 foreach ($found in $found_services)
			{
			 $serviceStatus = get-service -ComputerName $machineName -Name $found
			 
			 $svcName = $serviceStatus.name
			 $svcState = $serviceStatus.status
			 Add-Content $report "<tr>"
			 
			 if ($serviceStatus.status -eq "Running")
				{
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center><B>$machineName</B></td>"
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center><B>$svcName</B></td>"
				 Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
				 Write-Host $machineName `t $svcName `t $svcState -ForegroundColor "Green"
				}
			 else
				{
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"
				 Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"
				 Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
				 Write-Host $machineName `t $svcName `t $svcState -ForegroundColor "Red"
				 sc.exe \\$machineName config $found start= disabled
				 sc.exe \\$machineName delete $found
				}
			}

		 Add-Content $report "</tr>"
		}
	}
} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

Did that work?


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Script to delete Services not running

(OP)
Thanks for the help this is working great

RE: Script to delete Services not running

I'd like to just jump in and say that not all services run all the time... some services start only when needed...

I would be cautious just "deleting any service that isn't running". Or windows won't be running for long...

Just my $.02

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg

RE: Script to delete Services not running

(OP)
Correct. I have a specific list of services that are not windows services but third party application that need to run when computer is turned on.

Thank you for the cautionary statement

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close