简体   繁体   English

Powershell 脚本无法通过任务计划程序运行

[英]Powershell script does not run via Task Scheduler

PowerShell script runs fine manually, but doesn't run in Task scheduler. PowerShell 脚本手动运行良好,但不在任务调度程序中运行。 The script:剧本:

$env:CLIENTNAME | Out-File C:\Users\tst5clj\Desktop\folder\log.txt -Append

I tried another command($env:COMPUTERNAME) and works fine both manually and via PowerShell.我尝试了另一个命令($env:COMPUTERNAME),并且手动和通过 PowerShell 都可以正常工作。

If you want to run a.ps1 script from a scheduled task, your task's action should be configured as follows (at a minimum)如果您想从计划任务运行 a.ps1 脚本,您的任务操作应配置如下(至少)

  • Action: Start a program行动:启动程序
  • Program/script: powershell.exe程序/脚本:powershell.exe
  • Add arguments (optional): -executionpolicy bypass -file "{Absolute}\{Path}\{To}\script.ps1"添加 arguments(可选):-executionpolicy bypass -file "{Absolute}\{Path}\{To}\script.ps1"

And your script.ps1 can run whatever code you need.您的 script.ps1 可以运行您需要的任何代码。

# Setup for example
$env:CLIENTNAME = 'Contoso'

$env:CLIENTNAME | Out-File "$PSScriptRoot\log.txt"

Here's a similar Q&A: How to execute PowerShell script on Task Scheduler?这是一个类似的问答: 如何在任务计划程序上执行 PowerShell 脚本?

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

相关问题 通过任务计划程序的 Powershell 脚本不使用参数 - Powershell Script via Task Scheduler Not Working with Arguments Powershell脚本运行任务计划程序将无法启动Photoshop - Powershell script run task scheduler will not start photoshop 任务计划程序任务完成脚本无法运行 - Task Scheduler Task Completes Script Does not Run PowerShell 任务调度程序“CylancePROTECT”不执行脚本 - PowerShell script does not execute by task scheduler "CylancePROTECT" Powershell脚本无法使用Task Scheduler通过Outlook发送电子邮件 - Powershell Script not able to send email via Outlook using Task Scheduler 为什么任务计划程序的结果与手动运行PowerShell脚本不同 - Why the result of Task scheduler is different with manually run PowerShell script 如何在静默/隐藏模式下使用任务调度程序运行powershell脚本? - How to run powershell script using task scheduler in silent/hidden mode? 通过任务计划程序运行时,Azure Powershell脚本失败 - Azure Powershell script fails when run through task scheduler 从任务计划程序运行时,PowerShell脚本失败 - PowerShell script failing when run from the Task Scheduler 在网络位置上的任务计划程序中运行PowerShell脚本 - Run a PowerShell script in Task Scheduler that is located on network location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM