Solved

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

Posted on 2014-02-28
15
2,596 Views
Last Modified: 2014-05-28
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
Comment
Question by:eperez0968
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 16

Expert Comment

by:cantoris
ID: 39897365
(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
 

Author Comment

by:eperez0968
ID: 39897427
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
 
LVL 16

Expert Comment

by:cantoris
ID: 39897443
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
 

Author Comment

by:eperez0968
ID: 39897489
Going to try this and will post the results, I am scheduled for classes in Powershell but not till this Sept...
0
 
LVL 16

Accepted Solution

by:
cantoris earned 500 total points
ID: 39897490
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
 
LVL 16

Expert Comment

by:cantoris
ID: 39897500
(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
 

Author Comment

by:eperez0968
ID: 39897506
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
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
LVL 68

Expert Comment

by:Qlemo
ID: 39898388
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
 
LVL 16

Expert Comment

by:cantoris
ID: 39898441
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
 

Author Comment

by:eperez0968
ID: 39901912
didn't have the chance today to test, but I will tomorrow and will let you know my findings.
0
 

Author Comment

by:eperez0968
ID: 39911512
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
 
LVL 16

Expert Comment

by:cantoris
ID: 39912096
Service objects do have DependentServices and ServicesDependedOn properties which you could use but that would be quite a bit of extra work.
0
 

Expert Comment

by:im_busy
ID: 40094789
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 40095238
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now