![](/img/trans.png)
[英]Windows Task Scheduler to execute .aspx page using Powershell
[英]Powershell Scheduled Task Open Web Page to Execute Script
為了在Windows上像計划任務一樣設置“計划任務”,我使用了先前的stackoverflow問題建議的代碼來設置了Powershell腳本。
我有一些需要每天清理並刪除舊備份的備份,因此我創建了一個asp.net腳本來執行此任務-文件名為BackupCleanup.aspx,並且我已經確認ASP.net腳本在其上執行時可以正常工作通過訪問上面的URL自己擁有-但是我無法使用下面的Powershell腳本來執行它。
我正在使用的Powershell腳本代碼是:
$request = [System.Net.WebRequest]::Create("http://127.0.0.1/BackupCleanup.aspx")
$response = $request.GetResponse()
$response.Close()
我已經創建了一個帶有PS1擴展名的文件,它在我的操作系統中正確顯示(Windows 2008)-我已經嘗試通過右鍵單擊並選擇“使用Powershell運行”來手動執行此任務,並將其作為任務進行了安排-都徒勞無功。
我無法弄清楚該腳本為什么不起作用-任何幫助將不勝感激。
這是我用來使用IE調用網頁的Powershell腳本。 希望這也會為您服務。
Function NavigateTo([string] $url, [int] $delayTime = 100)
{
Write-Verbose "Navigating to $url"
$global:ie.Navigate($url)
WaitForPage $delayTime
}
Function WaitForPage([int] $delayTime = 100)
{
$loaded = $false
while ($loaded -eq $false) {
[System.Threading.Thread]::Sleep($delayTime)
#If the browser is not busy, the page is loaded
if (-not $global:ie.Busy)
{
$loaded = $true
}
}
$global:doc = $global:ie.Document
}
Function SetElementValueByName($name, $value, [int] $position = 0) {
if ($global:doc -eq $null) {
Write-Error "Document is null"
break
}
$elements = @($global:doc.getElementsByName($name))
if ($elements.Count -ne 0) {
$elements[$position].Value = $value
}
else {
Write-Warning "Couldn't find any element with name ""$name"""
}
}
Function ClickElementById($id)
{
$element = $global:doc.getElementById($id)
if ($element -ne $null) {
$element.Click()
WaitForPage
}
else {
Write-Error "Couldn't find element with id ""$id"""
break
}
}
Function ClickElementByName($name, [int] $position = 0)
{
if ($global:doc -eq $null) {
Write-Error "Document is null"
break
}
$elements = @($global:doc.getElementsByName($name))
if ($elements.Count -ne 0) {
$elements[$position].Click()
WaitForPage
}
else {
Write-Error "Couldn't find element with name ""$name"" at position ""$position"""
break
}
}
Function ClickElementByTagName($name, [int] $position = 0)
{
if ($global:doc -eq $null) {
Write-Error "Document is null"
break
}
$elements = @($global:doc.getElementsByTagName($name))
if ($elements.Count -ne 0) {
$elements[$position].Click()
WaitForPage
}
else {
Write-Error "Couldn't find element with tag name ""$name"" at position ""$position"""
break
}
}
#Entry point
# Setup references to IE
$global:ie = New-Object -com "InternetExplorer.Application"
$global:ie.Navigate("about:blank")
$global:ie.visible = $true
# Call the page
NavigateTo "http://127.0.0.1/BackupCleanup.aspx"
# Release resources
$global:ie.Quit()
$global:ie = $null
我遇到過同樣的問題。 我手動打開powershell並執行了腳本,然后收到“ WebPage.ps1無法加載,因為在此系統上禁用了運行腳本。”。
您必須允許腳本運行
在PowerShell中執行以下操作
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.