Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script to schedule daily task error

I have a very simple power shell script that will register console applications as daily scheduled tasks.

$TaskCommand = Read-Host 'Enter the path to the console application'
$TaskName = "TaskName"
$TaskStartTime = "10PM"
$TaskArg = "-WindowStyle Hidden -NonInteractive -Executionpolicy unrestricted"

$TaskAction = New-ScheduledTaskAction -Execute "$TaskCommand" -Argument "$TaskArg"
$TaskTrigger = New-ScheduledTaskTrigger -At $TaskStartTime -Daily
Register-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -TaskName "$TaskName" -User %computername%\theusername -Password "password" -RunLevel Highest

The application reads the file path from user input and attempts to register the application as a task using a specific user account. I can get the script working by using

-User "System"

However when I try to use the above script I get this error:

Register-ScheduledTask: No mapping between account names and security IDs was done.

I have ensured that the account exists as it is currently running several services. I am also new to powershell so have tried adding quotations around the username with no luck.

like image 813
jjharrison Avatar asked Jun 19 '17 13:06

jjharrison


People also ask

How to schedule a PowerShell script using Task Scheduler GUI?

1.Schedule PowerShell script using Task Scheduler GUI. 2.Schedule PowerShell script from Task Scheduler using PowerShell. Open Task Scheduler (it can be found in the ‘Administrative tools’ or by pressing ‘Windows+R’ to open run and then type “taskschd.msc”.) To schedule a script from Task Scheduler, follow these steps.

What is Task Scheduler and how does it work?

Task Scheduler is a Windows application that we can use to create and schedule any task. We can set our Task Actions to start programs, send emails, and display messages, or we can run any script – likely a PowerShell script since we’re on Windows. These Actions can be triggered on any schedule you configure your Scheduled Task to use.

How do I create a daily task in PowerShell?

schtasks.exe /CREATE /SC DAILY /MO 1 /TN 'task name' /TR 'powershell.exe C: est.ps1' /ST 07:00 /RI 30 /DU 24:00 This creates a task that runs daily, repeats every 30 minutes, for a duration of 1 day.

How to schedule a task in Windows 10?

Let’s start with the Task Scheduler. If you haven’t used Windows Task Scheduler, it is simple. If you don’t like it, there are free alternatives. System Scheduler by Splinterware is a nice alternative. Start by ope n ing the Task Scheduler. Create a basic task. Enter a name that is summarizes the task.


Video Answer


2 Answers

Don't think PowerShell recognises %computername%. Have a look at environment variables here.

$env:USERNAME, $env:USERDOMAIN $env:COMPUTERNAME look relevant to your task.

like image 106
G42 Avatar answered Sep 22 '22 06:09

G42


For me, it was an issue of having trailing spaces where the variables were stored. Unrelated to the question at hand, but perhaps a sanity-check worth pursuing for others who reads this.

like image 21
Morten Nørgaard Avatar answered Sep 23 '22 06:09

Morten Nørgaard