Script help - Restarting a service Daily

Hello All,
I would like some advice/help on creating a script which would restart a specfic service daily on a server- This may be required to run at a certain time - but finding it difficult getting started initially.
I am guesing I would need to create a scheduled task create a scheduled task that runs a script.ps1
script.ps1
stop-service "service"
start-service "�service"
Does anyone know of any written scripts that can get myself started - for example re-starting the print spooler script everyday at 08:00am etc.
Many thanks
Regards
QMU

I think you pretty much answered your own question. If you are just looking for a simple restart service script, then you already have it. You could look at using
Restart-Service instead as it will stop and start the service.
Restart-Service -Name 'Service'
All you need to do is create the scheduled job and specify powershell.exe as the program to run and then use
-File <path to script> as the arguments.
Lots of information about PowerShell and scheduled jobs here:
http://blogs.technet.com/b/heyscriptingguy/archive/tags/scheduled+tasks/default.aspx
Boe Prox
Blog |
Twitter
PoshWSUS |
PoshPAIG | PoshChat |
PoshEventUI
PowerShell Deep Dives Book
Hey Boe -
Many thanks mate - thanks for the link also.
I was just confused about the timing issue(which I thought would in script itself) and how to call the actual script - both which I am now able to do.
Many thanks
QMU

Similar Messages

  • [PowerShell] Script to restart a service and its dependents (and their dependents)

    I am trying to write a PowerShell script that will restart the SNMP Service on a server.  The server in question is an HP server that has the various HP Insight agents installed, and some of these depend on the SNMP Service.  One of these services
    also depends on one of the other HP Insight services (that in turn depends on SNMP Service).
    I can easily write a script that stops and starts the specific services in order, but I don't want to do that.  I like to create scripts that are as flexible as possible.  In this case, I'd like to write a script that takes a service name as input
    and can restart that service, including any dependents (and dependents of those dependents, and dependents of those dependents).  They way to do this seems to be recursion, but I simply cannot get it to work correctly.  Here is what I have so far
    (just trying to get the services to stop in the correct order):
    $MainServiceName = "SNMP"
    $ServiceInput = ""
    $CurrentService = ""
    $Service = ""
    function Custom-Stop-Service ($ServiceInput)
    Write-Host "Name of `$ServiceInput: $($ServiceInput.Name)"
    Write-Host "Number of dependents: $($ServiceInput.DependentServices.Count)"
    If ($ServiceInput.DependentServices.Count -gt 0)
    ForEach ($Service in $ServiceInput.DependentServices)
    Write-Host "Dependent of $($ServiceInput.Name): $($Service.Name)"
    If ($Service.Status -eq "Running")
    Write-Host "$($Service.Name) is running."
    $CurrentService = Get-Service -Name $Service.Name
    Custom-Stop-Service $CurrentService
    Write-Host "Stopping service $($CurrentService.Name)"
    Stop-Service -Name $CurrentService.Name -Force
    Else
    Write-Host "$($Service.Name) is stopped."
    Else
    Write-Host "Stopping service $($ServiceInput.Name)"
    Stop-Service -Name $ServiceInput.Name
    $MainService = Get-Service -Name $MainServiceName
    If ($MainService.Status -eq "Stopped")
    Write-Host "Service $($MainService.Name) is already stopped."
    Else
    Custom-Stop-Service $MainService
    However, it doesn't work like I expect.  Firstly, it never attempts to stop the SNMP service itself.  Secondly while it does seem to stop all the dependents of SNMP correctly, it tries to stop CqMgHost twice.  Here is the output:
    PS C:\Scripts> .\restart_service.ps1
    Name of $ServiceInput: SNMP
    Number of dependents: 4
    Dependent of SNMP: CqMgHost
    CqMgHost is running.
    Name of $ServiceInput: CqMgHost
    Number of dependents: 0
    Stopping service CqMgHost
    WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping...
    WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping...
    WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping...
    WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping...
    Stopping service CqMgHost
    Dependent of SNMP: CqMgStor
    CqMgStor is running.
    Name of $ServiceInput: CqMgStor
    Number of dependents: 1
    Dependent of CqMgStor: CqMgHost
    CqMgHost is stopped.
    Stopping service CqMgStor
    Dependent of SNMP: CqMgServ
    CqMgServ is running.
    Name of $ServiceInput: CqMgServ
    Number of dependents: 1
    Dependent of CqMgServ: CqMgHost
    CqMgHost is stopped.
    Stopping service CqMgServ
    WARNING: Waiting for service 'HP Insight Server Agents (CqMgServ)' to finish stopping...
    WARNING: Waiting for service 'HP Insight Server Agents (CqMgServ)' to finish stopping...
    Dependent of SNMP: CpqNicMgmt
    CpqNicMgmt is running.
    Name of $ServiceInput: CpqNicMgmt
    Number of dependents: 1
    Dependent of CpqNicMgmt: CqMgHost
    CqMgHost is stopped.
    Stopping service CpqNicMgmt
    Can someone suggest a different script or changes to my script so that it will do what I want?  I know that "Stop-Service -Name SNMP -Force" will work but there are two problems with this: 1) it doesn't record the stop events for the dependents in the
    Event Viewer and 2) it doesn't work in reverse ("Start-Service -Name SNMP -Force" nor "Restart-Service -Name SNMP -Force" restart the main service and the other services that depend on SNMP that were stopped).
    I'm a little surprised that Restart-Service doesn't behave like the restart function in the Services MMC does, where it easily and reliably handles the stops and starts of not only the service itself, but dependent services and also properly does event logging.

    I decided to try to understand why you see layers of dependencies.  I found that on WS2008 R2 HP managemnet does have a second layer.  It does not incluse anything but subservices from HP>  VSC is not  a dependency and, from my research,
    should not ever be a dependency.  Of course this does not stop someone from making it a dependency which could create issues.
    Here is a recursive function that will display all dependencies of any service or all services,
    The code returns the service names in order and can be used to restart all services in dependent order.
    function Get-Depends{
    [CmdLetBinding()]
    Param(
    $servicename='SNMP',
    $level = 0,
    [switch]$infoonly
    try{
    Write-Verbose $($("`t" * $level)+$servicename)
    $servicename
    $services=get-service $servicename -ea stop | where{$_.dependentservices}|select -expand dependentservices
    if($services){
    $services | %{Get-Depends $($_.Name) ($level+1) }
    catch{
    $_
    $depends=Get-Depends -v
    The verbose switch displays a hierarchical display of names to ilustrate the dependencies.
    jv

  • Need to restart AFP service daily

    Hi,
    A couple of days ago I verified and repaired disk permissions with Disk Utility on my Mac mini SLS. Now each morning users cannot see their shares on the network until I restart the AFP service. All the other services are working fine. What could be wrong with AFP? Any help for me?
    Thanks in advance.
    imacleo

    Is there a particular reason that you needed AFP turned on, on each of the client machines? If you're using the mini as a file share there shouldn't be a need for each client machine to be turned on. Turning on file sharing in System Preferences for a local machine is not needed to connect to a server share. It's only needed to share local folders or drives.

  • SCOM 2012 - Event ID 6024 (Launching Restart Health Service. Health Service exceeded Process\Handle Count or Private Bytes threshhold.)

    I am getting event ID 6024 (LaunchRestartHealthService.js : Launching Restart Health Service. Health Service exceeded Process\Handle Count or Private Bytes threshhold.) within an interval ranging from 12-17 minutes.
    I am using SCOM (2012 SP1 and 2012 R2) on Windows Server (2008 R2 / 2012 / 2012 R2).
    This issue is occurring only on agent managed computer (acting as proxy and discover managed objects on other computers setting is enabled) which i am using for monitoring my device. All discovery scripts (powershell) and monitors are targeted on this agent
    managed computer.
    There are total 80 discoveries and 900 monitors. 55 discoveries and 550 monitors are enabled by default and rest all are disabled.
    I am seeing event id 6024 frequently only on agent managed computer. Can anyone help me to resolve this issue.
    Thanks,
    Mukul

    To fix issue 6024, you can follow below steps:
    1. Open SCOM console. Go to Monitors -> Agent -> Entity Health -> Performance -> Health Service Performance -> Health Service State.
    2. Double click Health Service Handle Count Threshold monitor and go to Overrides page.
    3. Click Override -> For a specific object of Class: Agent. Select the affected SCOM agent QMXServer.
    4. Check on the parameter Agent Performance Monitor Type - Threshold. Change the default value 2000 to an appropriate value, like 4000. You can check the Health service handle count alert in SCOM console to get the value when the alert is generated. You
    can also launch the health explorer against QMXServer to check the value when the monitor state is changed from healthy to critical.
    Also you can refer below links
    http://blogs.technet.com/b/omx/archive/2013/10/17/health-service-restarts-on-service-manager-servers-with-scom-agents.aspx
    Please remember, if you see a post that helped you please click "Vote As Helpful" and if it answered your question, please click "Mark As Answer"
    Mai Ali | My blog: Technical | Twitter:
    Mai Ali

  • Restart a service on a remote computer

    So I am a little confused and would be gracious for some help here. In my PowerShell class I am tasked with the following:
    Write a PowerShell script that can be used to restart a service on a remote computer. The script must do the following:
    Accept a parameter to for the computer to restart the service on
    Display a list of services running on that computer
    Allow the user to enter the name of a service to restart
    Display a message that the service on the specified computer is about to be stopped
    Ask the user to hit the Enter key to continue
    Stop the service on the remote computer
    Display the state of the service on the remote computer after the stop command has been sent
    Display a message that the service on the specified computer is about to be started
    Ask the user to hit the Enter key to continue
    Start the service on the remote computer
    Display the state of the service on the remote computer after the start command has been sent
    I'm a little confused about how to set the variables into the script. Here is what I have so far but I'm not exactly sure of the errors im getting within the ISE. Any help would be greatly appreciated. Thanks
    $session = New-PSSession -ComputerName localhost
    Enter-PSSession $session
    Get-Service | sort-object -property `
    @{Expression="Status";Descending=$true}, `
    @{Expression="DisplayName";Descending=$false} | Where-Object {$_.status -eq "running"}
    $userinput = read-host "Enter Service to restart:"
    write-host "$userinput is about to Stop." -fore yellow -back magenta
    Write-Host "Press any key to continue ..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Stop-Service -InputObject (Get-Service -ComputerName "localhost" -Name "$userinput"

    Actually I will tell you the answer because you will not see it.
    The issue is you are using ISE when you should be using CLI or don't use
    $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    This command is specific to CLI PowerShell.
    ¯\_(ツ)_/¯

  • Scheduled webi reports fail, why? If I restart BO services they work again

    Hello.
    I'm having a problem with my scheduled Webi reports and I don't know what is causing it.  I have some reports scheduled to run at 8:00 am, and are supposed to be sent to a determined e-mail. Last week it worked well and the reports were sent to their destination successfuly, then this week the same reports didn't reach their destination.  I verified in the History option and all of them showed a Failed status.  I checked the detail and this is what it shows:
    Unexpected exception caught. Reason: [java.lang.NullPointerException: i_statusInfo is null.]
    If I try to reschedule these reports or any other report or a new report I get the same error.  The only way I can make the schedule option work again  for my webi reports is restarting  BusinessObjects services.
    But I guess this is not the right solution.
    What can I do? How can I determine what is causing my scheduled webi reports fail?
    What does  this java.lang.NullPointerException: i_statusInfo is null message mean?
    Is there a way I could track the service that is failing? which BO service is the service that manage the Schedule reports?
    Any help is welcome.
    Edited by: Erika Atencio on Sep 1, 2010 6:27 PM

    >
    Efstratios Karaivazoglou wrote:
    > Which version of BOBJ (incl. SP and FP) are you using?
    >
    > Regards,
    >
    > Stratos
    BusinessObjects XI 3.1 SP2
    FixPack 2.6
    Edited by: Erika Atencio on Sep 1, 2010 8:46 PM

  • How to manually restart a service

    systemuiserver hung up on me recently and after forcing it to quick in activity monitor, I could not figure out how to restart the service. I'm new to mac from windows, where I would just go to where the services are listen and click restart. I did try blindly using 'launchctl start systemuiserver', but it didn't find the tag systemuiserver. And i tried 'launchctl com.apple.systemuiserver' but that tag wasnt found either.
    Any ideas on how to restart a service without having to reboot?
    Thanks!

    What does the array contain? Objects or strings?
    Post some code... it might help!

  • How do I restart Cluster services?

    Can some one tell me ho do I restart Cluster Services?
    Name Type Target State Host
    ora....DB1.srv application ONLINE OFFLINE
    ora....MSDB.cs application ONLINE OFFLINE
    ora....B1.inst application ONLINE ONLINE fms-db1
    ora....B2.inst application ONLINE ONLINE fms-db2
    ora.FMSDB.db application ONLINE ONLINE fms-db2
    ora....B1.lsnr application ONLINE ONLINE fms-db1
    ora....db1.gsd application ONLINE OFFLINE
    ora....db1.ons application ONLINE ONLINE fms-db1
    ora....db1.vip application ONLINE ONLINE fms-db1
    ora....B2.lsnr application ONLINE ONLINE fms-db2
    ora....db2.gsd application ONLINE OFFLINE
    ora....db2.ons application ONLINE ONLINE fms-db2
    ora....db2.vip application ONLINE ONLINE fms-db2
    ????

    What did you mean Cluster Service?
    If you mean Oracle Cluster,
    1. You must root user.
    2. use crsctl command-line
    ./crsctl stop crs
    ./crsctl start crs
    Your Database and listener , they have resisted in Oracle Cluster, that down.
    If you mean database service. You can use srvctl command-line to help you

  • Give helpdesk ability to restart ALL SERVICES without making them local or domain administrators

    I have a windows server 2012 environment using Active directory.  I need to give my helpdesk group the ability to stop, start or restart all services on the machines located within an OU, but I do not want to make them local admins because I don't want
    them to be able to install software, create users etc. 
    I have found articles that allow you to grant this right on a PER SERVICE basis, but I am looking for a solution that allows them to restart any service...not just named services.  
    Any help would be greatly appreciated!
    -Brian

    > I have found articles that allow you to grant this right on a PER
    > SERVICE basis, but I am looking for a solution that allows them to
    > restart any service...not just named services.
    http://waynes-world-it.blogspot.de/2009/10/service-control-manager-security-for.html
    http://serverfault.com/questions/37721/how-do-i-grant-users-the-ability-to-install-windows-services
    Martin
    Mal ein
    GUTES Buch über GPOs lesen?
    NO THEY ARE NOT EVIL, if you know what you are doing:
    Good or bad GPOs?
    And if IT bothers me - coke bottle design refreshment :))

  • Ethernet won't start unless I run systemctl restart dhcpcd.service

    My ethernet connection won't start unless I do
    sudo systemctl restart dhcpcd.service
    I looked through the Arch Wiki and I don't feel like it goes over how to set up the ethernet and I couldn't find much in Google. Any help appreciated. Thanks.

    jasonwryan wrote:https://wiki.archlinux.org/index.php/Sy … mctl_usage
    I got it working now after reading that wiki page
    Just had to do
    systemctl enable dhcpcd.service
    Thanks!

  • [solved] How to restart all services with systemd?

    I had the habit of restarting all daemons after an upgrade so to make sure the newest libraries, configuration files etc were loaded. I did that with a shell script that restarted the daemons in order which they were listed in rc.conf. With systemd I could do something similar by first getting a list of running services and ordering a 'systemcrl restart  <servicename>' for each. But since systemd keeps track of dependencies I wonder if that would be the way to do it. Also I would need to filter out 'one shot' type services that are only needed on bootup.
    <edit>restart instead of reload</edit>
    <edit>solved it with a little script</edit>
    Last edited by rwd (2012-11-27 21:02:02)

    I think that's what systemctl snapshot does.
    snapshot [NAME]
               Create a snapshot. If a snapshot name is specified, the new snapshot will be named after it. If none is specified an automatic snapshot name is generated. In either case, the snapshot name used is printed to STDOUT, unless --quiet is specified.
               A snapshot refers to a saved state of the systemd manager. It is implemented itself as a unit that is generated dynamically with this command and has dependencies on all units active at the time. At a later time the user may return to this state by using the isolate command on the snapshot unit.
               Snapshots are only useful for saving and restoring which units are running or are stopped, they do not save/restore any other state. Snapshots are dynamic and lost on reboot.
    Then, isolate emergency.target or rescue target and switch to the snapshot.

  • Firefox not responding, freezes, sometimes responds after a few minutes, other times a pop-up appears asking if I want to stop script - help please

    firefox not responding, freezes, sometimes responds after a few minutes, other times a pop-up appears asking if I want to stop script - help please

    Start Firefox in [[Safe Mode]] to check if one of the add-ons is causing the problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).
    * Don't make any changes on the Safe mode start window.
    See:
    * [[Troubleshooting extensions and themes]]
    * [[Troubleshooting plugins]]
    If it does work in Safe-mode then disable all extensions and then try to find which is causing it by enabling one at a time until the problem reappears.
    * Use "Disable all add-ons" on the [[Safe mode]] start window to disable all extensions.
    * Close and restart Firefox after each change via "File > Exit" (Mac: "Firefox > Quit"; Linux: "File > Quit")

  • Facing an error while restarting the services : Process Manager is not initialized.

    while setting up the MDM environment I somehow messed up all the database connections.
    Now im unable to start or stop the services properly.
    Below is the error shown in the event viewer log for process MDM Ntier Process manager :
    The description for Event ID ( 3 ) in Source ( MDM NTier Process Manager ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: Exception Emdm_Exception with message 'Process Manager is not initialized.'.
    Kindly help
    I suspect this is because incorrect password for Engine login MDM_SYSTEM or incorrect configuration file (config.xml), However i may be wrong.

    As far as the "Process Manager is not initialized" error is concerned, we got the same error recently.
    What we did was restart the DRM services on the server - The N-Tier Client and the Web-Publisher.
    However, after doing it - we still got the error or DRM would not respond. On looking through it again, we saw that the Task Manager on the Server did not kill the DRM processes and so we did it manually. Then we restarted the services and it worked fine this time. See if this works for you - first kill all the services on the client sides and then on the server side, check for the client and server side process task manager and then restart the service. This worked for us, hopefully it works for you too.
    -- Adi

  • Script to restart dropbox

    I need to create a script to restart dropbox but I have no idea on bash programming.
    I have tried this:
    #!/bin/bash
    killall dropbox; sleep 5; dropboxd
    This closes the program but after that it says process terminated and doesn't restart.
    How can I tell this script to continue after killall to start dropbox again.
    Thanks, jose.
    Last edited by boina (2012-08-24 19:00:38)

    Thanks for your help. I made a few changes and when i run it on a terminal it works perfect. But when I cp the file to /et/pm/sleep.d/ to execute it when suspending it only closes dropbox but fails to load it again. Any ideas why?
    dropbos file:
    #!/bin/sh
    # Script para reiniciar dropbox despues de una suspencion
    case "$1" in
    hibernate|suspend)
    thaw|resume)
    killall dropbox && dropboxd
    esac

  • When do services fail, while start/restart the services

    Hi All,
    when do we have chance of services get fail while start/restart it (consider that everything is properly configured and instaled).
    and How do i troubleshoot it.
    can anyone come across related this issue, your experices will be helpful.
    Thanks in Advance.

    When Essbase.sec file got corrupted services will fail frequently.
    If you have back up of essbase.sec then replace it and restart the services or you need to create the essbase.sec file from starting.
    Regards
    Venkat G

Maybe you are looking for

  • No data in insight

    Hi. Please, give me advice what to do to find the solution and see data in insight dashboard (BPC for MS SP5). What i did: 1. i started insight service and set it to automatic 2. activated insight in bpc administration 3. made synchronization manuall

  • Official Apple Composite Cable DOESN'T WORK with iPhone 3GS firmware 3.1.2

    I've tried everything and I cannot make the official apple composite cable (MB129LL/B) work with my iPhone 3GS 3.1.2. I get no video output AND no sound as if the cable is simply not made for my 3GS What do need to do to get composite video output on

  • Update the System in C# WinForms with InstallShield

    I already created the application and install it on my own computer and other computers. I created the set up of my application using InstallShield 2013 and I want to update my system to the latest version. From version 1.00 to 1.01 . Is that possibl

  • What is General Error 49?

    I am trying to export a video out of Final Cut Pro, but constantly receive General Error 49. It does not matter how I export my video, as QuickTime Movie, QuickTime Conversion, through Compressor, I even tried to bring it into Motion, but Motion woul

  • Error on zipfile decompression (on method closeEntry)

    At 7/27/01 10:01 AM, ericdev wrote: Hi, I am trying to unzip a file of images to be later processed by JAI. When I Do a CloseEntry() method on a zip Entry it throws this exception. Any ideas? Are the files too big? I would think that java can handle