powershell - 使用计划的 Powershell 脚本调用 URL

标签 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()

当我从 Powershell ISE、Powershell 控制台或使用以下命令执行脚本时,该脚本可以正常工作:
powershell PATH_TO_MY_SCRIPT.ps1

但是当我从调度程序任务中执行它时它不起作用,它返回代码 0xFFFD0000。我发现了这个:http://www.briantist.com/errors/scheduled-task-powershell-0xfffd0000/
所以这可能是一个权限问题,所以我尝试了许多其他配置文件和选项(包括“以所有权限执行”)来测试,但没有成功。

我还执行了另一个 Powershell 脚本,它只是挂载了一个网络驱动器并复制了两个文件,我对这个脚本没有任何问题。所以我认为问题来自使用 .NET 对象来调用我的 URL。

我看到我可能必须在我的脚本中包含任何其他命令之前的模块,但我不知道我必须做什么。 (我不知道Powershell,我只是尝试用它来解决我的问题)。

谢谢你的帮助。

最佳答案

您需要将该日志文件放在共享目录中的某个位置。计划任务可能有也可能无权访问 $env:USERPROFILE .另外,请使用 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()

关于powershell - 使用计划的 Powershell 脚本调用 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17486205/

相关文章:

linux - 在没有 root 和特权用户的情况下安排长时间运行的 Laravel 任务的方法

python 调度 - 如何避免无限循环?

c# - 使用 C# 的任务计划程序(计划 .bat(批处理)文件)

powershell - 如何使用 PowerShell 脚本在 Windows 任务计划程序中配置 "If the task is already running, then the following rule applies"?

Powershell加载dll出现错误: Add-Type : Could not load file or assembly 'WebDriver.dll' or one of its dependencies.操作不受支持

powershell - 通过脚本(批处理/powershell)将网络文件/文件夹设置为离线

powershell - 向 csv 文件添加条目会引发错误 :does not contain a method named 'op_Addition'

java - 计划的执行者服务不工作

powershell - 在 PowerShell 脚本中访问 Windows 任务凭据

regex - 选择字符串结果上的 Powershell Select-Object 与 ForEach