powershell - 创建运行带有参数的 powershell 脚本的计划任务时出现问题
问题描述
我正在尝试将带有参数的 PowerShell 脚本作为计划任务执行。在“启动程序”屏幕上,我有
程序/脚本
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
并在添加参数
-Command "& C:\Test\MoveFiles.ps1 -destinationRoot \\OB-VM-ME-Data\ME-Data\Archived\Test"
我做错了什么?
编辑:附件是有问题的脚本
Param (
[Parameter(Mandatory=$true)][string]$destinationRoot
)
$path = (Get-Item -Path ".\").FullName
Get-ChildItem $path\* -Include *.bmp, *.svg | Where-Object {
$_.LastWriteTime -lt (Get-Date).AddDays(-30)
} | ForEach-Object {
$content = $path + "\" + $_.Name
$year = (Get-Item $content).LastWriteTime.Year.ToString()
$monthNumber = (Get-Item $content).LastWriteTime.Month
$month = (Get-Culture).DateTimeFormat.GetMonthName($monthNumber)
$destination = $destinationRoot + "\" + $year + "\" + $month
New-Item -ItemType Directory -Force -Path $destination
Move-Item -Path $content -Destination $destination -Force
}
解决方案
-Command
如果要执行 PowerShell 脚本(带或不带参数),请勿使用。改为使用-File
。将计划任务的参数列表更改为如下内容:
-File "C:\Test\MoveFiles.ps1" -destinationRoot "\\OB-VM-ME-Data\ME-Data\Archived\Test"
编辑:
我看不出你的脚本有什么本质上的错误。唯一可能证明有问题的是它试图从当前工作目录 ( Get-Item -Path ".\"
) 中读取文件,这可能是也可能不是您认为的那样。不过,您可以在计划任务设置中配置工作目录,以从等式中删除此变量。
由于计划任务是出了名的难以调试,甚至不清楚实际问题是什么或导致它的原因,您最好的选择可能是遵循我在回答类似问题时概述的调试步骤。
推荐阅读
- c# - 如何查看我的 Xamarin.Forms 应用程序是否在 Visual Studio 中的 Android 6(或更高版本)上运行或/和可运行?
- netflix-zuul - 是否可以将 Netflix Zuul 中的响应的最大标头大小从 8KB 增加到 32KB?
- powershell - kakfa本地shell窗口关闭时间
- python - 获取当前会话的用户名
- ios - Plists 不再是标准做法了吗?
- javascript - 使用固定时间字符串获取当前日期 - 时刻 js
- python - 父进程中断时如何完成子进程的工作?
- c++ - 使用 cpprestdk 的 C++ HTTP 请求总是异常。没有为 cpprest142_2_10d.dll 加载符号
- vue.js - vue组件库的正确使用方法是什么?
- monitoring - 有没有办法在 PromQL 中进行类似查询的“左外连接”?