locked
Log file not being generated while running a powershell script via Windows Task Scheduler RRS feed

  • Question

  • I have created the following powershell script:

    stop-service ''3456''

    start-sleep -s 60

    stop-service "2354"

    start-sleep -s 60

    Restart-computer QY34 -Force

    send -mailmessage -from operating.system@abc.com -To asdf@abc.com -subject test -attachment 'c:\Temp|test.log' -smtp server "127.0.0.1"

    I have entered the following information on windows task scheduler

    On Actions Tab;

    C:\windows\System32\windowspowershell\v1.0\powershell.exe

    Arguments Tab:

    -file "c:\scripts\test.ps1"*>"c:\Temp\test.log"

    Can anyone  please help me in getting the log file while running the script via Task Scheduler?

    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:49 PM This is not scheduler troubleshooting forum
    Wednesday, April 3, 2019 6:31 PM

All replies

  • -file c:\scripts\test.ps1 > c:\Temp\test.log

    NO quotes.  The command line des not understand *> syntax. 

    You are better of generating a file in the PS1 script.  The output from your commands are not available as written.

    The sleep commands are pointless.  The service commands will not return until the service has changed.

    There are numerous syntax errors in your code.  If you would correctly write, format and test your code you would see most of the issues.

    Here is how to post code in this forum:

    $logfile = 'c:\Temp\test.log'
    $mailprops = @{
        From       = 'operating.system@abc.com'
        To         = 'asdf@abc.com'
        Subject    = 'test'
        Attachment = $logfile
        SmtpServer = '127.0.0.1'
    }
    
    'Stopping services' | Out-File $logfile
    Stop-Service '3456','2354'
    'Services stopped' | Out-File $logfile -append
    Restart-computer QY34 -Force
    'Remote computer sent restart command' | Out-File $logfile -append
    Send-MailMessage @mailprops
    
    

    Of course there are a dozen other technical things you will need to learn to write a script and run it as a task.  If you are trying to restart the computer running the task then that will not work.



    \_(ツ)_/

    Wednesday, April 3, 2019 7:23 PM