none
Powershell Scheduled Tasks by Author RRS feed

  • Question

  • Hello,

    I have the code below that will export all scheduled tasks from the root folder. Can someone point me in the direction of what I would need to do to modify the code so it only exports tasks created by a Specific Person.

    I only want the tasks created by certain users, so I can export it to XML and re-import to another server

    $sch = New-Object -ComObject("Schedule.Service")

    $sch.Connect("localhost")
    $tasks = $sch.GetFolder("\").GetTasks(0)

    $outfile_temp = "D:\temp\Tasks\{0}.xml"

    $tasks | %{
      $xml = $_.Xml
      $task_name = $_.Name
      $outfile = $outfile_temp -f $task_name
      $xml | Out-File $outfile
    }

    Saturday, January 14, 2017 3:39 PM

Answers

  • ok, I go it to work by using the code below

    Only thing I'm not sure is what the -f actually stands for , It appears it is sometime of a replacement command to update the path variable and remove the {0} and replace with task_name

    Can anyone point me to a web page that explains that.

    Thanks for everyone's Help

    $outfile = $outfile_temp -f $task_name

    $tasks=Get-ScheduledTask |where author -like "master"
    $outfile_temp = "D:\Users\Paul Schrader\Documents\Scheduled Tasks\Tasks\{0}.xml"
    
    $tasks | Foreach-Object{
     
      $task_name = $_.TaskName
    
      $outfile = $outfile_temp -f $task_name
    
    
    Get-ScheduledTask $task_name|Export-ScheduledTask|out-file $outfile
     
    
    }


    Sunday, January 15, 2017 5:06 AM

All replies

  • What version of Windows and Powershell do you have? With Windows 10 you live could be much easier
    Get-ScheduledTask | Select-Object -Property TaskName,Author


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Saturday, January 14, 2017 6:39 PM
    Saturday, January 14, 2017 4:58 PM
  • $sch = New-Object -ComObject ("Schedule.Service")
    $sch.Connect('localhost')
    $sch.GetFolder('\').GetTasks(0) |
    	ForEach-Object{ [xml]$_.Xml } | 
    	ForEach-Object{
    		[pscustomobject]@{
    			TaskName = $_.Task.RegistrationInfo.Uri
    			Author = $_.Task.RegistrationInfo.Author
            }
        }
    

    Use Get-ScheduledTask if you are on a newer system.


    \_(ツ)_/

    Saturday, January 14, 2017 7:14 PM
  • A less elegant brute force method:

    $sch.GetFolder('\').GetTasks(0) |
    	ForEach-Object{ [xml]$_.Xml } |
    	Select -Expand Task | 
    	Select -Expand RegistrationInfo | 
    	Select Author, URI


    \_(ツ)_/

    Saturday, January 14, 2017 7:18 PM
  • I have windows 10
    Saturday, January 14, 2017 7:25 PM
  • ... and ... did you try the suggested code?  ... mine or jrv's?

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Saturday, January 14, 2017 8:18 PM
  • im trying both but missing how to get it to export to xml just those that are created by specfic user

    Below is where I'm at , I have ll of the Scheduled tasks create by myself in variable $RT, now trying to tweak my orig code to export each one to an xml file


    PS C:\WINDOWS\system32> $rt= Get-ScheduledTask|where author -eq "TR\Paul Schrader"

    PS C:\WINDOWS\system32> $rt

    TaskPath                                       TaskName                          State     
    --------                                       --------                          -----     
    \                                              backup scheduled tasks            Ready     
    \                                              bbm_test                          Ready     
    \                                              Copy BBM                          Ready     
    \                                              Listing                           Ready     
    \                                              PowerShell_ISE.exe_1827274155     Ready     
    \                                              ps test                           Ready     
    \                                              Reboot                            Ready     
    \                                              tbi_to_vhdx                       Ready     
    \                                              xx1                               Ready     

    Saturday, January 14, 2017 8:18 PM
  • Export-ScheduledTask

    If you learn how to use help and read it, it will give you all of these answers with examples.


    \_(ツ)_/

    Saturday, January 14, 2017 8:32 PM
  • You really should start to learn the basics of Powershell.

    Get-ScheduledTask | Where-Object {$_.TaskPath -eq '\' -and $_.Author -like '*Superman*'}

    That's how you get all tasks from the root created by superman. ;-)

    BTW: Do yourself and us a favor and format your code as code here in the forum. you can use the option in the edit bar for it.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Saturday, January 14, 2017 8:32 PM
  • Humble apologies, this is my first time using Ps

    I'm trying to get the basics but I have no foundation to build it from.

    Was hopping to get some help here on how to complete what I thought would be a simple task, so I could use that example and break it into pieces and get a better understanding of PS

    Basically wanted to get all tasks created by a specific user and then take that results set and export the tasks in xml format to my local hard drive.

    I've got the first part where I can get the tasks created by a users, but am having no luck doing a loop to export the task details in xml format to local hard drive.

    I will continue to try and figure this out.

    Regards

    Paul

    Saturday, January 14, 2017 9:07 PM
  • Depending what for you like to extract these information there are different ways to store it in files. The most common way would be csv I think. You can use Export-CSV for this.

    There are some great free sources in the Internet to start learning Powershell:

    http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start

    http://www.microsoftvirtualacademy.com/training-courses/advanced-tools-scripting-with-powershell-3-0-jump-start

    https://www.youtube.com/playlist?list=PL6D474E721138865A

    https://www.youtube.com/results?search_query=don+jones+toolmaking

    https://www.youtube.com/watch?v=SSJot1ycM70

    And ... last but not least ... as jrv already mentioned: you should always read the complete help for the cmdlets you are about to use ... including the examples. That can avoid a lot of frustration for you.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Saturday, January 14, 2017 9:32 PM
  • ok, I go it to work by using the code below

    Only thing I'm not sure is what the -f actually stands for , It appears it is sometime of a replacement command to update the path variable and remove the {0} and replace with task_name

    Can anyone point me to a web page that explains that.

    Thanks for everyone's Help

    $outfile = $outfile_temp -f $task_name

    $tasks=Get-ScheduledTask |where author -like "master"
    $outfile_temp = "D:\Users\Paul Schrader\Documents\Scheduled Tasks\Tasks\{0}.xml"
    
    $tasks | Foreach-Object{
     
      $task_name = $_.TaskName
    
      $outfile = $outfile_temp -f $task_name
    
    
    Get-ScheduledTask $task_name|Export-ScheduledTask|out-file $outfile
     
    
    }


    Sunday, January 15, 2017 5:06 AM
  • Is this what you are trying to do?

    $path = 'D:\Users\Paul Schrader\Documents\Scheduled Tasks\Tasks\' #'
    Get-ScheduledTask | 
    	where author -match 'master' |
    	Export-ScheduledTask |
    	ForEach-Object{Out-File $path+$_.TaskName+'.xml'}
    I recommend taking the time to actually learn PowerShell instead of just trying to guess at how to use it.  It will save you a lot of time and may prevent some potentially serious disasters.


    \_(ツ)_/




    • Edited by jrv Sunday, January 15, 2017 9:40 AM
    Sunday, January 15, 2017 9:37 AM
  • help about operators

    \_(ツ)_/

    Sunday, January 15, 2017 9:42 AM