我有一个监控图像文件夹的PowerShell脚本.我需要找到一种在计算机启动后自动运行此脚本的方法.
我已经尝试了以下方法,但我无法使其正常工作.
使用msconfig并将PowerShell脚本添加到启动,但我找不到该列表上的PowerShell脚本.
创建快捷方式并将其拖放到启动文件夹.没运气.
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)
或%SystemRoot%\ system32\WindowsPowerShell\v1.0\powershell.exe -File"C:\ Doc\Files\FileMonitor.ps1"
这是我的PowerShell脚本:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)我还尝试使用taskschd.msc添加基本任务.它仍然无法正常工作.
这是我发现的,也许这将有助于调试它.
如果我打开PowerShell窗口并在那里运行脚本,它就可以工作了.但如果我在CMD提示中运行它,
$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
}
Run Code Online (Sandbox Code Playgroud)
不起作用.我不确定这是许可问题还是其他问题.
顺便说一下,我安装了PowerShell 3.0,如果输入$ host.version,它会在那里显示3.但我的powershell.exe似乎仍然是v1.0.
powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)小智 45
我终于让我的PowerShell脚本在每次启动时自动运行.您需要创建两个文件:第一个是Powershell脚本(例如script.ps1),第二个是.cmd文件,它将包含将在命令提示符下运行的命令(例如startup.cmd).
第二个文件是计算机启动时需要执行的操作,只需将.ps1复制粘贴到启动文件夹就行不通,因为它实际上并不执行脚本 - 它只用记事本打开文件.您需要执行.cmd,它本身将使用PowerShell执行.ps1.好的,足够的bab呀学语和步骤:
C:\ Users\< user_name >\Desktop\script.ps1
C:\ Users\< user_name >\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd
执行此操作将在每次启动时执行cmd文件. 以下是如果您需要帮助时如何创建.cmd文件的链接.
使用文本编辑器打开.cmd文件,然后输入以下行:
script.ps1
startup.cmd
这将做两件事:
此代码专门用于PowerShell v1.0.如果你正在运行PowerShell v2.0,它可能会有所不同.在任何情况下,请检查此源代码以获取.cmd代码.
现在您已经在各自的路径中使用了.ps1和.cmd文件,并且每个文件的脚本都已设置完毕.
mjo*_*nor 11
您可以将其设置为计划任务,并为"启动时"设置任务触发器
小智 8
You could create a Scheduler Task that runs automatically on the start, even when the user is not logged in:
schtasks /create /tn "FileMonitor" /sc onstart /delay 0000:30 /rl highest /ru system /tr "powershell.exe -file C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)
Run this command once from a PowerShell as Admin and it will create a schedule task for you. You can list the task like this:
schtasks /Query /TN "FileMonitor" /V /FO List
Run Code Online (Sandbox Code Playgroud)
or delete it
schtasks /Delete /TN "FileMonitor"
Run Code Online (Sandbox Code Playgroud)
我要做的是创建一个放置在shell:startup中的快捷方式。
快捷方式具有以下内容:
目标: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "C:\scripts\script.ps1"
(用您需要的替换scripts \ scripts.ps1)
开始于: C:\scripts
(用包含您脚本的文件夹替换脚本)
小智 7
试试这个:在启动文件夹中创建一个快捷方式并输入
PowerShell "& 'PathToFile\script.ps1'"
Run Code Online (Sandbox Code Playgroud)
这是最简单的方法。
这实际上只是对@mjolinor 简单答案[使用任务计划程序]的扩展。
我知道“任务计划程序”是正确的方法,但我花了一些精力让它按照我想要的方式运行,并认为我会为其他人发布我的发现。
问题包括:
注意:您必须具有运行脚本的权限,请参阅 ExecutionPolicy
然后在任务计划程序中,最重要/最棘手的部分是 Action
它应该是 Start a Program
程序/脚本:
powershell
Run Code Online (Sandbox Code Playgroud)
添加参数(可选):
-windowstyle hidden -command full\path\script.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
笔记:
如果您-File在互联网上看到它会起作用,但-File除了文件路径之外什么都不能理解,IE:重定向被视为文件路径的一部分并且失败,您必须-command与重定向一起使用,但是您可以预先添加其他命令/参数,例如-windowstyle hidden不显示 PowerShell 窗口。
我不得不调整都Write-Host对Write-Output我的剧本也是如此。
| 归档时间: |
|
| 查看次数: |
115593 次 |
| 最近记录: |