Invoke-webrequest script to run a .jsp script

Chamandeep Singh 41 Reputation points
2021-09-05T23:39:01.79+00:00

Hi,

OS = Windows Server 2019

I need to setup a task in Windows task scheduler. This task scheduler will run a powershell script using 'Invoke-WebRequest'

Invoke-WebRequest will run a script which is http://X.X.X.X:8080/DeleteLog/DeleteLog.jsp. This script is also on the same server where I need to create task.

Can please someone help me creating the powershell script using Invoke-WebRequest

Also .jsp script will clear the logs on same server

Thanks

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2021-09-06T12:52:53.823+00:00

    Create a scripts folder with a logs subfolder. Use notepad and create a file named C:\Scripts\ClearLog.ps1 with this content.

    $LogFile = "C:\Scripts\Logs\ClearLog.log"                         # Your log file
    $WebPage = "http://X.X.X.X:8080/DeleteLog/DeleteLog.jsp"       # Your page to call 
    
    "{0} - Calling log clear page." -f (Get-Date) | out-file $LogFile
    try {
        $Result = Invoke-WebRequest -Uri $WebPage -ErrorAction Stop
        "{0} - Status code is {1}" -f (Get-Date), $Result.StatusCode    | out-file $LogFile -Append
        # Review $Result.content for page output.
    }
    catch {
      "{0} - We crashed:  {1}" -f (Get-Date), $_  | out-file $LogFile -Append
    }
    "{0} - Script end." -f (Get-Date)  | out-file $LogFile -Append
    

    Define a task and have it run:

    Powershell.exe  -ExecutionPolicy Bypass -file C:\Scripts\ClearLog.ps1
    

    Set the task to run as the SYSTEM account initially. Run the task and review the log file.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-09-06T11:42:41.823+00:00

    Hello

    You can find details for the cmdlet and some examples in:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.1

    If you are facing an specific error while running the cmdlet come back and post the script and some screenshot or output so the community can help you further.

    Hope this helps!
    Best regards,

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.