2017-04-27 227 views
-1

我已经尝试过每个教程,但是在设置任务时不会运行。Powershell不从计划任务运行

有人能告诉我如何获得计划的任务在Windows 10上运行PowerShell脚本?

编辑:

一些额外的信息:

我试图运行一个脚本,删除超过10天以上的文件。在这里得到一些帮助。

How can I delete files with PowerShell without confirmation?

$Days = "10" 
#----- define folder where files are located ----# 
$TargetFolder = "D:\Shares\Downloads\TV\AutoDL" 
#----- define LastWriteTime parameter based on $Days ---# 
$LastWrite = (Get-Date).AddDays(-$Days) 

#----- get files based on lastwrite filter and specified folder ---# 
$Files = Get-Childitem $TargetFolder -Recurse -file | Where LastWriteTime -le "$LastWrite" 

if ($Files -eq $null) 
{ 
    Write-Host "No more files to delete!" -foregroundcolor "Green" 
} 
else 
{ 
    $Files | %{ 
    write-host "Deleting File $_" -ForegroundColor "DarkRed" 
    Remove-Item $_.FullName -Force | out-null 
    } 

} 

在计划任务我有这些设置:我曾尝试 “的PowerShell”, “powershell.exe” 和“C:

最高privliges 程序/脚本运行\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe“

添加参数:我试过”。\ nameofscript.ps1“, - 文件”C:\ Script \ nameofscrips.ps1“, - 命令和其他我发现的建议。

开始在:C:\脚本

我试过设置在这些教程:

https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script/

https://www.metalogix.com/help/Content%20Matrix%20Console/SharePoint%20Edition/002_HowTo/004_SharePointActions/012_SchedulingPowerShell.htm

+0

请提供你的脚本是做一些更多的信息,以及如何计划任务已配置代码和代码使我们能够提供帮助。说你试过的教程意味着人们可以消除那些对你无用的东西。 – gms0ulman

回答

0

你有没有设置执行策略?

使用管理员PowerShell和执行:

Set-ExecutionPolicy Unrestricted 

或者你设置目录的脚本位置“在启动”?

0

我们确实需要更多关于脚本的信息才能提供帮助。我遇到的最大问题是与外部对象交互的脚本。例如,发送电子邮件消息,通过PowerShell在Word或Excel中运行宏等。我通过调用批处理文件或将其设置为在有人登录时运行来解决此问题。另一个我见过的问题如果我没有明确说明路径。 H:\script.ps1\\server\file\myhomedrive\script.ps1.

而且,我称之为使用启动程序,程序/脚本C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe参数-file "\\server\file\script.ps1

只需使用PowerShell.exe的程序/脚本从任务调度程序脚本已经工作在斑点状最好的,总是用如果可能的完整路径。

最后,当计划任务运行时,它应该给你一个最后运行结果。你那里有什么?

+0

上次运行结果给我0x1。我在原始文章中更新了一些附加信息。 – ProphetSe7en

相关问题