Powershell
--
Questions
--
Followers
Top Experts
I have a PS script which sets a PS scheduled task BUT it lacks some things = setting username to run with + 'run whether user is logged on or not', configure task for Windows 2012 R2 instead of default ".
Can't figure out howto do this in Powershell.
Please advise.
This is the script:
function new-PSScheduledTask
{
param(
[Parameter(Mandatory = $true)][string[] $Computername,
[Parameter(Mandatory = $true)][string] $ScriptPath,
[Parameter(Mandatory = $true)][string]$Functionto
[Parameter(Mandatory = $true)][string]$Taskname,
[Parameter(Mandatory = $false)]$TriggerTime = '6:00'
)
invoke-command -ScriptBlock {
$TriggerFrequency = @{ Daily = $true }
$Trigger = New-ScheduledTaskTrigger -At $using:TriggerTime @TriggerFrequency
$commandtoexecute = "& {. $using:ScriptPath;$using:f
$argument = "-NoProfile -WindowStyle Hidden -command $commandtoexecute"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $argument
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $using:TaskName -Description $using:TaskName -RunLevel Highest #-User $UsernameTorunScheduledJob
} -ComputerName $Computername
Write-Output "TO SET: username to run with + 'run whether user is logged on or not', configure for Windows 2012 R2 "
}
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I believe that if you save the password, the task will run whether the user is logged in or not.
Cannot validate argument on parameter 'UserId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
Command is
new-PSScheduledTask -Computername servername -ScriptPath 'e:\myscript.pst' -FunctiontoExecute 'Get-something' -Taskname 'sometask'
then it prompts for user and password but it throws an error.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Cannot validate argument on parameter 'UserId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {get-eventlog -logname Application -After ((get-date).AddDays(-1)) | Export-Csv -Path c:\Reports\applog.csv -Force -NoTypeInformation}"'
$trigger = New-ScheduledTaskTrigger -Daily -At 11am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AppLog" -Description "Daily dump of Applog"
here's the source: https://social.technet.microsoft.com/Forums/lync/en-US/d87da3b2-ea3b-4b68-82f4-be0bf685446b/powershell-to-create-schedule-task-on-windows-10?forum=winserverpowershellSo It should be related to the task itself, so the error should be within the Scriptblock:
#$action = New-ScheduledTaskAction -Execute foo.exe -Argument "bar baz"
$principal = New-ScheduledTaskPrincipal -UserId $UserName -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel
$TriggerFrequency = @{ Daily = $true }
$Trigger = New-ScheduledTaskTrigger -At $using:TriggerTime @TriggerFrequency
$commandtoexecute = "& {. $using:ScriptPath;$using:functiontoexecute}"
$argument = "-NoProfile -WindowStyle Hidden -command $commandtoexecute"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $argument
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $using:TaskName -Description $using:TaskName -RunLevel Highest -Principal $UserName -Settings $settings -Password $pwd
Will check it later.
Powershell
--
Questions
--
Followers
Top Experts
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.