Eric Perez
asked on
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
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
ASKER
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
It's a basic example but here you go:
If you're new to PowerShell, get the two excellent "Month of Lunches" books on the subject from Manning.
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"
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.
ASKER
Going to try this and will post the results, I am scheduled for classes in Powershell but not till this Sept...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(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/
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/
ASKER
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 :)
Some small improvements to above code (not really important, though):
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.
$pingable = $false
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
$pingable = $true
}
can be written short as $pingable = Test-Connection -ComputerName $computer -Count 1 -Quiet
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
}
and line 40 as
$props["Success"] = $success
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.
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.
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.
ASKER
didn't have the chance today to test, but I will tomorrow and will let you know my findings.
ASKER
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...
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...
Service objects do have DependentServices and ServicesDependedOn properties which you could use but that would be quite a bit of extra work.
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.
Simply change lines 14-17 of the accepted answer or my simplification line to
$pingable = $true
That then requires no further change.
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).