Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to delete Services not running

Status
Not open for further replies.

cojast08

Technical User
Apr 4, 2008
25
US
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
 
What do you have so far?


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
############################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)

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

}

}


}

}
 
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.
 
Did that work?


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top