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.
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.
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.
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.
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.
Don't think PowerShell recognises %computername%
. Have a look at environment variables here.
$env:USERNAME
, $env:USERDOMAIN
$env:COMPUTERNAME
look relevant to your task.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With