• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3684
  • Last Modified:

PowerShell Script to stop / start services from a txt file of servers

Hi, I am new to PowerShell and I am looking for a PowerShell script to do the following.

1. read from a txt file of all servers
2. Check to see if you can remote PowerShell to server if not turn on that feature
3. Stops / Start IIS Service on windows 2008 r2 and or be able to select what service or multiple services you would like to stop or start.
4. and output the results of each server to excel or HTML if it successfully stopped or started the service you requested
0
Eric Perez
Asked:
Eric Perez
  • 6
  • 5
  • 2
  • +1
1 Solution
 
cantorisCommented:
(1) is done with Get-Content
eg
Get-Content c:\Servers.txt | ForEach-Object {
   $server = $_
   # do something to $server
}

(2) you should be doing with Group Policy.  You can't use PowerShell to enable PowerShell remoting ... remotely!

(3) this you'd add in to the ForEach scriptblock above
eg
(Get-Service -ComputerName $server -Name <service name here>).Start()

I've only got one PC to hand at the moment so can't check, so if that doesn't work against a remote PC, you'd have to use the WMI Win32_Service class instead.

(4) you could do with a PSObject and some error trapping inside the script block instead of the basic step 3 above to catch the results and make something usable.
eg
Try {
   Get-Service etc as above
   $success = $true
} catch {
   $success = $false
}
$props = @{
   "ComputerName" = $server;
   "Service1StartResult" = $success
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj

Then at the end of the pipeline (ie the end of the ForEach-Object scriptblock), pipe the results to Export-Csv (for Excel) or ConvertTo-Html | Out-File (for HTML).
0
 
Eric PerezAuthor Commented:
Seems like you have it all broken up, can you put it all together as if it we're ready to run, I would like to see how an example would look like
0
 
cantorisCommented:
It's a basic example but here you go:
Get-Content c:\Servers.txt | ForEach-Object {
   $server = $_
   try {
      (Get-Service -ComputerName $server -Name BITS).Start()
      $success = $true
   } catch {
      $success = $false
   }
   $props = @{
      "Computer Name" = $server;
      "BITS Service Started OK" = $success
   }
   $obj = New-Object -TypeName PSObject -Property $props
   Write-Output $obj
} | ConvertTo-Html | Out-File "c:\Output.html"

Open in new window

There is of course loads more you could add to this - test the servers are online before connecting to them or even turn the whole lot into a complex function where you give it a list of services and whether to start or stop them as a passed parameter.

If you're new to PowerShell, get the two excellent "Month of Lunches" books on the subject from Manning.
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
Eric PerezAuthor Commented:
Going to try this and will post the results, I am scheduled for classes in Powershell but not till this Sept...
0
 
cantorisCommented:
OK here's something with a lot more meat to it:
function Invoke-ServerServiceChanges {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [string[]]$ComputerName="localhost",

        [Parameter(Mandatory=$true)]
        [hashtable]$operations
    )
 
    PROCESS {
        $ComputerName | ForEach-Object {
            $computer = $_
            $pingable = $false
            if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
                $pingable = $true
            }
            foreach ($service in $operations.Keys) {
                $operation = $operations[$service]
                $props = @{
                    "ComputerName" = $computer;
                    "Online" = $pingable;
                    "Service" = $service;
                    "Operation" = $operation
                }
                if ($pingable) {
                    $success = $true
                    try {
                        if ($operation -eq "Stop") {
                            (Get-Service -ComputerName $computer -Name $service).Stop()
                        } else {
                            (Get-Service -ComputerName $computer -Name $service).Start()
                        }
                    } catch {
                        $success = $false
                    }
                } else {
                    $success = $false
                }
                $props.Add("Success",$success)
                $obj = New-Object -TypeName PSObject -Property $props
                Write-Output $obj
            }
        }
    }
}

# Hashtable of services and whether to Start or Stop them.
$actions = @{
    "BITS"="Start";
    "WUAUServ"="Stop"
}

# Could get server list from a text file:
# $servers = Get-Content "C:\Servers.txt"
# or pass in the names directly:
$servers = "Server1","Server2"
$servers | Invoke-ServerServiceChanges -operations $actions | Sort ComputerName,Service | Select ComputerName,Online,Service,Success | ConvertTo-Html | Out-File "C:\Output.html"

Open in new window

0
 
cantorisCommented:
(I just tried the above against a remote machine and it worked.)

Don't wait until September!
Get this: http://www.manning.com/jones3/
And if you want to write something like the longer code above, get this: http://www.manning.com/jones4/
0
 
Eric PerezAuthor Commented:
I will look into the links you sent, you are absolutely correct I should not wait long... I have over 800 windows 2008 r2 servers I will test this on my lab which I can test 200 servers...that's why I like to use a text file of all the servers and go through each and give a report :)
0
 
QlemoDeveloperCommented:
Some small improvements to above code (not really important, though):
            $pingable = $false
            if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
                $pingable = $true
            }

Open in new window

can be written short as
            $pingable = Test-Connection -ComputerName $computer -Count 1 -Quiet

Open in new window

Re $prop, it is much better to create objects with all necessary properties or hash keys from start than to dynamically add them, so I would write line 20-25 as
                $props = @{
                    "ComputerName" = $computer;
                    "Online" = $pingable;
                    "Service" = $service;
                    "Operation" = $operation
                    "Success" = $false
                }

Open in new window

and line 40 as
                $props["Success"] = $success

Open in new window

The reason for that is that changing an objects metadata always results in a new object. If you do a lot of it, that involves a lot of overhead for memory allocation and garbage collection.

It is disputable if you really would like to have each service listed if the machine is not reachable by a ping. I for myself would only create a single entry per machine in that case.
0
 
cantorisCommented:
Thanks Qlemo. I know fiddling with strings makes new strings each time but didn't know the same applied to calling methods on a hashtable.  Then again, I'm not a real developer!  Is there an easy way to see where this sort of thing is the case?  Perhaps from the Windows SDK?

I did umm and ahh a bit about how best to present the output but decided that at least like this, the output could easily be Selected to omit unpingable machines and that always having one service per output object would make it easier to deal with the output consistently than perhaps using an array or hashtable as one of the fields.
0
 
Eric PerezAuthor Commented:
didn't have the chance today to test, but I will tomorrow and will let you know my findings.
0
 
Eric PerezAuthor Commented:
ok got some info, it worked perfectly, the only issue is when a service has dependencies, it asks if you want to shut those down as well, so thinking about that issue if you would start that service how will it start the other dependencies?

1. if a  service that you select to stop has dependencies, stop them as well
2. start up the service with all its dependencies.

if the service doesn't have any dependencies it works perfectly...
0
 
cantorisCommented:
Service objects do have DependentServices and ServicesDependedOn properties which you could use but that would be quite a bit of extra work.
0
 
im_busyCommented:
i'm also interested in this script but icmp is blocked, I cannot use test-connection and don't know enough about ps scripting to edit.
0
 
QlemoDeveloperCommented:
Simply change lines 14-17 of the accepted answer or my simplification line to
$pingable = $true

Open in new window

That then requires no further change.
0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now