我已尝试以下方法,但均未成功:
1. 使用 msconfig 并将 PowerShell 脚本添加到启动项中,但在该列表中找不到 PowerShell 脚本。 2. 创建快捷方式并将其拖到启动文件夹。没有成功。
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
或者%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
这是我的PowerShell脚本:
$folder = "C:\\Doc\\Files"
$dest = "C:\\Doc\\Files\\images"
$filter = "*.jpg"
$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubDirectories=$false
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
Start-Sleep -s 10
Move-Item -Path C:\Doc\Files\*.jpg C:\Doc\Files\images
}
我还尝试使用taskschd.msc
添加基本任务,但它仍然无法运行。
这是我发现的情况,也许可以帮助调试:
如果我在 PowerShell 窗口中打开并运行脚本,则可以正常工作。但如果我在命令提示符中运行,
powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
无法运行。我不确定是权限问题还是其他问题。
顺便说一下,我已经安装了 PowerShell 3.0,如果我键入 $host.version,它会显示 3。
但是我的 powershell.exe 似乎仍然是 v1.0。
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe