简体   繁体   中英

Powershell script does not run via Task Scheduler

PowerShell script runs fine manually, but doesn't run in Task scheduler. 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.

If you want to run a.ps1 script from a scheduled task, your task's action should be configured as follows (at a minimum)

  • Action: Start a program
  • Program/script: powershell.exe
  • Add arguments (optional): -executionpolicy bypass -file "{Absolute}\{Path}\{To}\script.ps1"

And your script.ps1 can run whatever code you need.

# 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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