使用计划的Powershell脚本调用URL

Dor*_*ian 11 powershell scheduled-tasks

我只想在Windows Server 2008和Powershell脚本上调用带有任务调度程序的URL.

所以我开发了以下脚本:

$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()
Run Code Online (Sandbox Code Playgroud)

当我从Powershell ISE,Powershell控制台或使用命令执行它时,该脚本没有任何问题:

powershell PATH_TO_MY_SCRIPT.ps1
Run Code Online (Sandbox Code Playgroud)

但是当我从Scheduler任务执行它时它不起作用,它返回代码0xFFFD0000.我发现这一点:http://www.briantist.com/errors/scheduled-task-powershell-0xfffd0000/ 因此,它可以是一个权限的问题,所以我尝试了很多其他的配置文件和选项(包括"所有权限执行"),以测试,没有成功.

我还执行其他PowerShell脚本刚刚安装网络驱动器复制两个文件,我没有与该脚本任何问题.所以我认为问题来自使用.NET对象来调用我的URL.

我看到了,我可以有任何其他命令之前,包括在我的脚本模块,但我不知道到底是什么我必须做的.(我不知道Powershell,我只是试着用它来解决我的问题).

谢谢你的帮助.

Loï*_*HEL 14

我在计划任务中使用了以下内容,它按预期工作:

$url="http://server/uri"
(New-Object System.Net.WebClient).DownloadString("$url");
Run Code Online (Sandbox Code Playgroud)