简体   繁体   English

使用PowerShell将脚本作为隐藏的计划任务运行

[英]Run a script as hidden scheduled task with PowerShell

I want the word files on the desktop to be moved to the local disk D at the time I set each day. 我希望每天设置的时间将桌面上的word文件移动到本地磁盘D中。 I've created a scheduled task and code works as follows. 我创建了一个计划任务,代码工作如下。

$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Invoke-Command -ScriptBlock { Move-Item -Path $home\Desktop\*.doc -Destination D:\ }'
$trigger = New-ScheduledTaskTrigger -Daily -At 05:30pm
Register-ScheduledTask -TaskName "Task" -Action $action -Trigger $trigger -RunLevel Highest -Force

But there is a problem. 但有一个问题。 I want this task timer to be done in a hidden way, without appearing on the PowerShell window. 我希望此任务计时器以隐藏的方式完成,而不会出现在PowerShell窗口中。

To solve this problem I also wanted a way like the following. 为了解决此问题,我还希望采用以下方法。

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-ExecutionPolicy Bypass -NoExit -NoProfile -WindowStyle Hidden Invoke-Command -ScriptBlock { Move-Item -Path $home\Desktop\*.doc -Destination D:\ }'
$trigger = New-ScheduledTaskTrigger -Daily -At 05:30pm
Register-ScheduledTask -TaskName "Task" -Action $action -Trigger $trigger -RunLevel Highest -Force

Would you help me fix the code I wrote to make the scheduled task run without the PowerShell window appearing? 您是否可以帮助我修复编写的代码以使计划的任务运行而不显示PowerShell窗口?

To run a scheduled task in the background configure it to run whether the user is logged in or not. 要在后台运行计划任务,请将其配置为无论用户是否登录均运行。 Since you only want to copy files from one local disk to another I'd also recommend not storing the password. 由于您只想将文件从一个本地磁盘复制到另一个本地磁盘,因此我也建议您不要存储密码。 Invoke-Command should't be required either. 也不需要Invoke-Command Just run Move-Item directly. 只需直接运行Move-Item

$action    = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Move-Item ...'
$trigger   = New-ScheduledTaskTrigger -Daily -At 05:30pm
$principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType "S4U" -RunLevel Highest

Register-ScheduledTask -TaskName "Task" -Action $action -Trigger $trigger -Principal $principal -Force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM