15

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.

Dorian
  • 761
  • 3
  • 11
  • 28
  • 1
    I think it's more likely that the problem is with accessing the `USERPROFILE` directory - it may not be what you expect it to be. It could also be an issue with your proxy setting (or lack thereof) if it's an external URL. What user account does the scheduled task execute under? – alroc Jul 05 '13 at 11:14
  • Can not agree more with @alroc on the 'USERPROFILE' part. When you run it manually (or interactively) it is whoever logged into the system and open the session (Eg. if you log in as "Peter" you will get "Peter"'s profile). When run it through schedule task then it will be "SYSTEM"s profile by default which could make `${Env:USERPROFILE}\Desktop\Planification.log` no sense. But "SYSTEM" account can be changed though: In "Scheduled task" Control Panel right mouse click it and then go properties, you will be able to modify the user account under which the task runs. – Peter Jul 05 '13 at 15:19
  • 1
    Detailed answer for PowerShell 3+ or older versions: https://stackoverflow.com/a/12081686/155687 – Vladislav Apr 11 '19 at 07:01

3 Answers3

19

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

$url="http://server/uri"
(New-Object System.Net.WebClient).DownloadString("$url");
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • The difference between your and Dorian's is whether using variable in another variable's definition. – Peter Jul 05 '13 at 15:31
3

following code should do what you need.

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

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()
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91