Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an URL with Scheduled Powershell Script

I just want to call an URL with the tasks scheduler on Windows Server 2008 and a Powershell Script.

So I developed the following script :

$url = "http://example.com"

$log_file = "${Env:USERPROFILE}\Desktop\Planification.log"
$date = get-date -UFormat "%d/%m/%Y %R"
"$date [INFO] Exécution de $url" >> $log_file

$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
$response.Close()

The script works without any issue when I execute it from the Powershell ISE, the Powershell console or with the command :

powershell PATH_TO_MY_SCRIPT.ps1

But it doesn't works when I execute it from the Scheduler tasks, it returns the code 0xFFFD0000. I found this : http://www.briantist.com/errors/scheduled-task-powershell-0xfffd0000/ So it can be a permission problem, so I tried many others profiles and option (including "Execute with all permissions") to test, without success.

I execute also another Powershell script which just mount a network drive and copy two files, and I don't have any issue with this script. So I think that the issue come from the using of the .NET object to call my URL.

I saw that I may have to include modules in my script before any other commands, but I don't know exactly what I have to do. (I don't know Powershell, I just try to use it for resolving my problems).

Thank you for you help.

like image 960
Dorian Avatar asked Jul 05 '13 10:07

Dorian


People also ask

How do I trigger a PowerShell script remotely?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.

How do I run a PowerShell script with administrator privileges in task scheduler?

In the Task Scheduler, define the task to run as an account that is a member of the administrators group. To prevent UAC issues, select "run with highest privileges". You don't need to do that. Just have the 1st PS run the script.


1 Answers

i used the following in a scheduled task and it works as expected :

$url="http://server/uri"
(New-Object System.Net.WebClient).DownloadString("$url");
like image 120
Loïc MICHEL Avatar answered Sep 21 '22 15:09

Loïc MICHEL