How to start a job in Register-ScheduledJob

i have use Register-ScheduledJob

and register a job ,


Register-ScheduledJob -ScriptBlock $setu 

and could get the schedulejob with Get-ScheduledJob

Get-ScheduledJob

Id         Name            JobTriggers     Command                                  Enabled   
--         ----            -----------     -------                                  -------   
1          setu            0                (Invoke-WebRequest https://www.jilig... True      



problem is how can i start to job manualy in the schedulejob?

I think just to run it as a manual one off, Start-Job can do it: Start-Job (Microsoft.PowerShell.Core) - PowerShell | Microsoft Learn. Looks like you might have to pass it the Type parameter of PSScheduledJob.

If you want to do something on a schedule I think you need a job trigger. Take a look at: about Scheduled Jobs - PowerShell | Microsoft Learn and New-JobTrigger (PSScheduledJob) - PowerShell | Microsoft Learn

i am sorry ,i was trying to use the Register-ScheduledJob and run the job without trigger

i try to use Get-ScheduledJob and get the command and run it but fail

& (Get-ScheduledJob -id 1 | Select-Object -Property Command).command
& : The term ' (Invoke-WebRequest https://www.jiligamefun.com/category/photo ).links|ForEach-Object {$_.outertext + $_.href}' is not recogni
zed as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t
hat the path is correct and try again.
At line:1 char:3
+ & (Get-ScheduledJob -id 1 | Select-Object -Property Command).command| ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ( (Invoke-WebReq...text + $_.href}:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The call operator won’t work with that command structure. You can either use Invoke-Expression to run the string as a command:

Invoke-Expression (Get-ScheduledJob -Name 'Test').Command

or use the call operator, but convert the String to a ScriptBlock:

& ([ScriptBlock]::Create((Get-ScheduledJob -Name 'Test').Command))
1 Like

thanks a lot ,thanks to you i am able to register the prompt and don’t need to write the extra ps1 file for it

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.