none
ScheduledJob scriptblock failing RRS feed

  • Question

  • 

    I'm writing a script involving scheduledjob cmdlets which opens files that users want at a specific time. I'm able to schedule a job to run a scriptblock which opens .bat and .mp3 but I couldn't use the scriptblock to open other extension file (.txt, .doc, .mp4, .ps1). It's like this:

    These commands worked fine:

    register-scheduledjob -name abc -trigger $abc -scriptblock {start "d:\folder\a.mp3"}

    register-scheduledjob -name abc -trigger $abc -scriptblock {start "d:\folder\a.bat"}

    But these commands failed:

    register-scheduledjob -name abc -trigger $abc -scriptblock {start "d:\folder\b.txt"}

    register-scheduledjob -name abc -trigger $abc -scriptblock {start "d:\folder\b.doc"}

    register-scheduledjob -name abc -trigger $abc -scriptblock {start notepad}

    In the scriptblock I have tried using invoke-item and invoke-command but none of those succeeded. I then checked task manager to find running process, it turned out that those files I have failed to open were running in the background with no windows opened

    Why does it happen? Do you guys have any solution? Please help me. It took me very long time trying to fix that problem! Thanks in advance


    • Edited by Eikacy Thursday, October 1, 2015 1:07 PM
    Thursday, October 1, 2015 1:07 PM

Answers

  • Hi Eikacy,

    "A lot of Windows PowerShell details are exposed in the Scheduled Tasks view of a job to handle the Windows PowerShell specifics that scheduled job cmdlets handle in the background."

    In short: Register-ScheduledJob defaults to Hidden PowerShell.exe window. Which can be modified using GUI or Set-ScheduledTask.

    Detailed:

    These are the steps used to cofirm this and set a job to be visible.

    Register-ScheduledJob -Name SCJob1 -ScriptBlock {ipconfig}
    
    #List all the RegisteredJobs
    
    PS system32> Get-ScheduledJob
    
    Id         Name            JobTriggers     Command                                  Enabled
    
    --         ----            -----------     -------                                  -------
    
    1          SCJob1          0               ipconfig                                 True
    
    5          SCJob2          0               ipconfig                                 True
    
    #Notice the path in GUI is 'TaskSch\Microsoft\Windows\PowerShell\ScheduledJobs'
    
    #Hidden is not selected in GUI.
    
    PS system32> Get-ScheduledJob SCJob1 | fl
    
    #Note it uses PowerShell.exe and has set to -WindowStyle Hidden, hence task visible doesn't matter and is still hidden.
    
    PS system32> Get-ScheduledJob SCJob1 | fl PSExecutionArgs
    
    PSExecutionArgs : -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef =
    
                      [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('SCJob1',
    
                      'C:\Users\satyajit.baruah\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs'); $jobDef.Run()"
    
    #You can't overwrite this default set value
    
    PS system32> (Get-ScheduledJob SCJob1).PSExecutionArgs = $Newarg
    
    'PSExecutionArgs' is a ReadOnly property.
    

    So, now in GUI you can see the Job’s ‘Actions’ tab that the PowerShell.exe  has ‘-WindowStyle Hidden’ as arguments.

    You can manually edit in Windows7,Windows2008 (I’m not able to find any set PS cmdlets yet for PSExecutionArgs)

    For Windows2012, Win8 onwards you can use the Cmdlet Set-ScheduledTask and New-ScheduledTaskAction to update the Arguments.

    #Locate the Args using other cmdlet
    
    Get-ScheduledTask SCJob1 | select -ExpandProperty Actions
    
    (Get-ScheduledTask SCJob1).Actions
    
    “-NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob….”
    
    #Collect the current Actions in variable
    
    $action = (Get-ScheduledTask SCJob1).Actions
    
    #Create a updated Action without hidden
    
    $NewAction = New-ScheduledTaskAction -Execute $action.Execute -Argument $action.arguments.Replace("-NonInteractive -WindowStyle Hidden","")
    
    PS C:\Windows\system32> $NewAction
    
    Id               :
    
    Arguments        : -NoLogo -Command "Import-Module...
    
    Execute          : powershell.exe
    
    #Update the Job with the new Action arguments, make sure TaskPath is mentioned
    
    PS C:\Windows\system32> Set-ScheduledTask "SCJob1" -TaskPath \Microsoft\Windows\PowerShell\ScheduledJobs\ -Action $NewAction
    
    TaskPath                                       TaskName                          State
    
    --------                                       --------                          -----
    
    \Microsoft\Windows\PowerShell\ScheduledJobs\   SCJob1                            Ready
    

    References and more info:

    When to use scheduled tasks vs. scheduled jobs:

    http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell.aspx

    Set-ScheduledTask:

    https://technet.microsoft.com/en-us/library/jj649814(v=wps.620).aspx

    PowerShell Jobs Week: Scheduled Jobs:

    http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/05/powershell-jobs-week-scheduled-jobs.aspx


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    • Marked as answer by Eikacy Tuesday, October 6, 2015 5:05 PM
    Monday, October 5, 2015 9:21 AM

All replies

  • Without an exact example of the script that causes the issue we cannot guess at a solution.


    \_(ツ)_/

    Thursday, October 1, 2015 6:39 PM
  • Thank you. I have 2 solutions from stackoverflow.

    First one is: 

    $principal = New-ScheduledTaskPrincipal -LogonType Interactive -UserId $env:USERDOMAIN\$env:USERNAME
    Set-ScheduledTask -TaskName abc -TaskPath Microsoft\Windows\PowerShell\ScheduledJobs -Principal $principal

    Second one is:

    schtasks /change  /it /tn "Microsoft\Windows\PowerShell\ScheduledJobs\music1"
    /rp computerpassword

    But I still don't know how to prompt user for password like

    schtasks /change  /it /tn "Microsoft\Windows\PowerShell\ScheduledJobs\music1"
    /rp (get-credential -username $env:username -message "Enter password")

    It returns error wrong user name or password, seems like the /rp doesn't accept secure string password.

    Can you help me with this?

    Friday, October 2, 2015 6:26 AM
  • /rp uses a plain text password.


    \_(ツ)_/

    Friday, October 2, 2015 5:32 PM
  • Hi Eikacy,

    "A lot of Windows PowerShell details are exposed in the Scheduled Tasks view of a job to handle the Windows PowerShell specifics that scheduled job cmdlets handle in the background."

    In short: Register-ScheduledJob defaults to Hidden PowerShell.exe window. Which can be modified using GUI or Set-ScheduledTask.

    Detailed:

    These are the steps used to cofirm this and set a job to be visible.

    Register-ScheduledJob -Name SCJob1 -ScriptBlock {ipconfig}
    
    #List all the RegisteredJobs
    
    PS system32> Get-ScheduledJob
    
    Id         Name            JobTriggers     Command                                  Enabled
    
    --         ----            -----------     -------                                  -------
    
    1          SCJob1          0               ipconfig                                 True
    
    5          SCJob2          0               ipconfig                                 True
    
    #Notice the path in GUI is 'TaskSch\Microsoft\Windows\PowerShell\ScheduledJobs'
    
    #Hidden is not selected in GUI.
    
    PS system32> Get-ScheduledJob SCJob1 | fl
    
    #Note it uses PowerShell.exe and has set to -WindowStyle Hidden, hence task visible doesn't matter and is still hidden.
    
    PS system32> Get-ScheduledJob SCJob1 | fl PSExecutionArgs
    
    PSExecutionArgs : -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef =
    
                      [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('SCJob1',
    
                      'C:\Users\satyajit.baruah\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs'); $jobDef.Run()"
    
    #You can't overwrite this default set value
    
    PS system32> (Get-ScheduledJob SCJob1).PSExecutionArgs = $Newarg
    
    'PSExecutionArgs' is a ReadOnly property.
    

    So, now in GUI you can see the Job’s ‘Actions’ tab that the PowerShell.exe  has ‘-WindowStyle Hidden’ as arguments.

    You can manually edit in Windows7,Windows2008 (I’m not able to find any set PS cmdlets yet for PSExecutionArgs)

    For Windows2012, Win8 onwards you can use the Cmdlet Set-ScheduledTask and New-ScheduledTaskAction to update the Arguments.

    #Locate the Args using other cmdlet
    
    Get-ScheduledTask SCJob1 | select -ExpandProperty Actions
    
    (Get-ScheduledTask SCJob1).Actions
    
    “-NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob….”
    
    #Collect the current Actions in variable
    
    $action = (Get-ScheduledTask SCJob1).Actions
    
    #Create a updated Action without hidden
    
    $NewAction = New-ScheduledTaskAction -Execute $action.Execute -Argument $action.arguments.Replace("-NonInteractive -WindowStyle Hidden","")
    
    PS C:\Windows\system32> $NewAction
    
    Id               :
    
    Arguments        : -NoLogo -Command "Import-Module...
    
    Execute          : powershell.exe
    
    #Update the Job with the new Action arguments, make sure TaskPath is mentioned
    
    PS C:\Windows\system32> Set-ScheduledTask "SCJob1" -TaskPath \Microsoft\Windows\PowerShell\ScheduledJobs\ -Action $NewAction
    
    TaskPath                                       TaskName                          State
    
    --------                                       --------                          -----
    
    \Microsoft\Windows\PowerShell\ScheduledJobs\   SCJob1                            Ready
    

    References and more info:

    When to use scheduled tasks vs. scheduled jobs:

    http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell.aspx

    Set-ScheduledTask:

    https://technet.microsoft.com/en-us/library/jj649814(v=wps.620).aspx

    PowerShell Jobs Week: Scheduled Jobs:

    http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/05/powershell-jobs-week-scheduled-jobs.aspx


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    • Marked as answer by Eikacy Tuesday, October 6, 2015 5:05 PM
    Monday, October 5, 2015 9:21 AM
  • thank you I was able to to this by (get-credential -username $env:username -message "Enter password").GetNetworkCredential().password
    Tuesday, October 6, 2015 5:05 PM
  • Yeah that helps, thanks so much
    Tuesday, October 6, 2015 5:06 PM