stopping services with powershell

Fun with Powershell - Stopping services remotely

Powershell is something can you get addicted to very fast, if you know what  you are doing! So this morning I was messing around with the "read-host" cmdlet and I wanted to try something with services. I wanted to ask the user what service he/she wanted to restart and on what computer. After that I wanted to go ahead and restart that service, assuming you have the privileges on the remote machine and then display a status message. Its not a ground breaking script but something to do for fun! (Well, don't go about restarting services in your production environment now, just coz I have mentioned the "fun" bit!)

You can extend the below script by checking if the user has entered a valid service name or a valid computer name and if not you can display a warning message. I leave that part to you and if you do extend it, please comment below and let me know!

$service = read-host "What is the service you want to STOP?"
$computer = read-host "What is the server name you want to STOP this service on?"
Get-Service $service -ComputerName $computer | Stop-Service -Force
Get-Service $service -ComputerName $computer
$svc = Get-Service -ComputerName $computer -Name $service
write-host "`n"
if ($svc.Status -eq "Stopped"){
Write-Host "Looks like you have stopped the service, using the $env:userdomain\$env:username account" -ForegroundColor Green
write-host "SORRY, the service could not be stopped :-("