-2

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.

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
  • Question is a little confusing... Is the entire script you are trying to run just printing the environment variable 'CLIENTNAME' to a file? How are you scheduling this in Task Scheduler? Is there any error or output? Is the environment variable 'CLIENTNAME' actually set on the system? It is set on the User? – DVS Aug 10 '20 at 14:11

1 Answers1

0

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?

derekbaker783
  • 8,109
  • 4
  • 36
  • 50