patriots
asked on
batch service restart
I'd like to restart a single service on a half dozen Windows 2003/2008 servers or more. I'd like to do this by a script, and would prefer to have this script query a plain text list of server names. If not a script a utility that can do the same would be great as well. I know such a thing can be done easily with a utility like Hyena from SystemTools, but we don't have that tool here.
I'd prefer PowerShell to do this, but have not seen much detailed information on how to perform this type of command remotely and at the same time reference a list of servers in txt or csv format.
I'd prefer PowerShell to do this, but have not seen much detailed information on how to perform this type of command remotely and at the same time reference a list of servers in txt or csv format.
ASKER
Thanks, seems to work well. When you run it, it returns this output at the powershell prompt, but it does what it's supposed to do. What does this output mean?
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
Ah sorry, forgot about that. Lets make it prettier.
Chris
Get-Content YourList.txt | ForEach-Object {
$Service = Get-WmiObject Win32_Service -Computer $_ -Filter "Name='dnscache'"
$Stop = $Service.StopService()
$Start = $Service.StartService()
If ($Stop.ReturnValue -eq 0 -And $Start.ReturnValue -eq 0) {
Write-Host "Successfully restarted DNS Client on $_" -ForegroundColor Green
} Else {
Write-Host "Failed to stop or start DNS Client on $_" -ForegroundColor Red
}
}
Better?Chris
ASKER
This might be more complex, but I can see where it would be helpful to pipe the output results to a log file, with more meaningful output...for example, text like "<service name> restarted successfully on <server name>"
Ultimate goal is that we have a backup service on a varying list of servers that periodically needs to be restarted in order to attempt to fix possible back up issues. The aim is to make it so we can drop the troulbesome list of servers into the text file, run the script, and have some verification that it succeeded without having to manually check each server. (Of course if we have to manually check, it more or less defeats the purpose of automating the process.)
Thank you. I'm a novice at powershell, but anxious to learn it.
Ultimate goal is that we have a backup service on a varying list of servers that periodically needs to be restarted in order to attempt to fix possible back up issues. The aim is to make it so we can drop the troulbesome list of servers into the text file, run the script, and have some verification that it succeeded without having to manually check each server. (Of course if we have to manually check, it more or less defeats the purpose of automating the process.)
Thank you. I'm a novice at powershell, but anxious to learn it.
ASKER
The prettier code works even better. It's almost perfect. The only thing that's not right on it is that one of the servers in my list I do not have access to b/c my user account happens to not have permission to the system. It generates this error, but then proceeds to say the service restarted successfully:
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At D:\scripts\adsmrestart\ads mrestart.p s1:2 char:27
+ $Service = Get-WmiObject <<<< Win32_Service -Computer $_ -Filter "Name='ServiceName"
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessExceptio n
+ FullyQualifiedErrorId : System.UnauthorizedAccessE xception,M icrosoft.P owerShell. Commands.G etWmiObjec tCommand
Successfully restarted ServiceName on <SERVERNAME>
The script needs to check if "access is denied" and change the message at the end stating such and then it should work perfectly for me.
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At D:\scripts\adsmrestart\ads
+ $Service = Get-WmiObject <<<< Win32_Service -Computer $_ -Filter "Name='ServiceName"
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessExceptio
+ FullyQualifiedErrorId : System.UnauthorizedAccessE
Successfully restarted ServiceName on <SERVERNAME>
The script needs to check if "access is denied" and change the message at the end stating such and then it should work perfectly for me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works perfectly. Your a genius. Thanks.
Cross-posted, this version introduces basic error handling so we can figure out if we can actually do things with the service at all.
I've got comments in the script, but if you want more explanation please do let me know.
I've got comments in the script, but if you want more explanation please do let me know.
$ServiceName = "DNS Client"
Get-Content YourList.txt | ForEach-Object {
# Get the service
$Service = Get-WmiObject Win32_Service -Computer $_ -Filter "Caption='$ServiceName'" -ErrorAction SilentlyContinue
# Test to see if that command worked and we have our service. $? is a special variable that tells us if the last command worked or not
If ($?) {
# If it worked: Stop then Start
$Stop = $Service.StopService()
$Start = $Service.StartService()
# Figure out if what we tried worked
If ($Stop.ReturnValue -eq 0 -And $Start.ReturnValue -eq 0) {
$Status = "Succeeded"
} Else {
$Status = "Failed"
}
} Else {
# Get-WmiObject didn't work, so...
$Status = "Failed to connect to service"
}
# Prepare some output for us
New-Object PsObject -Property @{
Server = $_;
ServiceName = $ServiceName;
Restart = $Status;
}
}
Chris
Open in new window
If you want to see the list of services you can play with on a given computer stick with:Open in new window
It's a long list, but you can make "-Filter" use any of the fields you see. For example, a more friendly name for the service name used above:Open in new window
HTHChris