question

ChamandeepSingh-6114 avatar image
0 Votes"
ChamandeepSingh-6114 asked MotoX80 answered

Invoke-webrequest script to run a .jsp script

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello

You can find details for the cmdlet and some examples in:
https://docs.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,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.