我有一个 powershell 脚本,每天下载防病毒更新。当我手动执行脚本时,它运行良好,但在计划任务中不起作用。只打开包含脚本代码的 .ps 文件(它在记事本中打开)。我不知道发生了什么。这是脚本:
remove-item E:\Update\* -Recurse
#Proxy auth
$Username="daril.aleman"
$Password="MyPassword"
$WebProxy = New-Object
System.Net.WebProxy("http://proxy.example.com:3128",$true)
$url="The.url.of.download.com/file.zip"
$client = new-object System.Net.WebClient
$client.Proxy=$Webproxy
$client.proxy.Credentials = New-Object
System.Net.NetworkCredential($Username, $Password)
$client.DownloadFile($url, "E:\Update\Daily_Update.zip")
Set-Location E:\Update\
$Unzip = New-Object -ComObject Shell.Application
$FileName = "Daily_Update.zip"
$ZipFile = $Unzip.NameSpace((Get-Location).Path + "\$FileName")
$Destination = $Unzip.namespace((Get-Location).Path)
$Destination.Copyhere($ZipFile.items())
答案1
以下是您需要的设置:
行动:启动程序
程序/脚本:命令行工具
添加参数:-ExecutionPolicy 绕过 -FILE "C:\path\script.ps1"
例子:https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler