简体   繁体   English

使用计划的Powershell脚本调用URL

[英]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. 我只想在Windows Server 2008和Powershell脚本上调用带有任务调度程序的URL。

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 ISE,Powershell控制台或使用命令执行它时,该脚本没有任何问题:

powershell PATH_TO_MY_SCRIPT.ps1

But it doesn't works when I execute it from the Scheduler tasks, it returns the code 0xFFFD0000. 但是当我从Scheduler任务执行它时它不起作用,它返回代码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. 我发现了这个: http//www.briantist.com/errors/scheduled-task-powershell-0xfffd0000/所以它可能是一个权限问题,所以我尝试了很多其他配置文件和选项(包括“执行所有权限”)到测试,没有成功。

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. 我还执行另一个Powershell脚本,它只挂载一个网络驱动器并复制两个文件,我对这个脚本没有任何问题。 So I think that the issue come from the using of the .NET object to call my URL. 所以我认为问题来自使用.NET对象来调用我的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). (我不知道Powershell,我只是试着用它来解决我的问题)。

Thank you for you help. 谢谢你的帮助。

i used the following in a scheduled task and it works as expected : 我在计划任务中使用了以下内容,它按预期工作:

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

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 . 计划任务可能有也可能无法访问$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, eg hostname | Write-Host ). 此外,使用Start-Transcript让PowerShell将脚本的输出写入日志文件( STDOUT除外,您需要将可执行文件的输出传递给Write-Host,例如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()

following code should do what you need. 以下代码应该做你需要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM