Call an URL with Scheduled Powershell Script

14

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.

powershell
scheduled-tasks
asked on Stack Overflow Jul 5, 2013 by Dorian • edited Jul 5, 2013 by Dorian

3 Answers

16

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

$url="http://server/uri"
(New-Object System.Net.WebClient).DownloadString("$url");
answered on Stack Overflow Jul 5, 2013 by Loïc MICHEL
3

following code should do what you need.

$url = "http://www.google.com"
PowerShell Invoke-WebRequest -Uri $url -Method GET 
1

You'll want to put that log file out somewhere in a shared directory. Scheduled tasks may or may not have access to $env:USERPROFILE. Also, use Start-Transcript to have PowerShell write your script's output to a log file (except for STDOUT, you'll need to pipe the output from executables to Write-Host, e.g. hostname | Write-Host).

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

$log_file = "C:\PlanificationLogs\Planification.log"
Start-Transcript -Path $log_file

Get-Date -UFormat "%d/%m/%Y %R"
Write-Host "$date [INFO] Exécution de $url" 

# PowerShell 3
Invoke-WebRequest -Uri $url

# PowerShell 2
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
$response.Close()
answered on Stack Overflow Jul 9, 2013 by Aaron Jensen

User contributions licensed under CC BY-SA 3.0