locked
Scheduled Task - Trigerred by Event - Getting data into the scripts as parameters...- Question RRS feed

  • Question

  • Hello - I have event ID - let say password reset (4720 or any other). I have attached the task to the event, so whenever admin is resetting a password the task can trigger power-shell script. Now - the question: How to pass the data from this event using task scheduler into the power-shell script? Thanks for help...
    Thursday, September 17, 2015 1:37 PM

Answers

  • Just in case some one in the future will be looking for this:

    1. Export xml from task

    2. Add this to xml:

    <ValueQueries>
            <Value name='TargetUserName'>Event/EventData/Data[@Name="TargetUserName"]</Value>
        </ValueQueries> 

    3. Add this to the script being run by the task:

    param(
    $TargetUserName
    )

    And do something with $TargetUserName

    4. Add this to the task (parameter box):

    C:\script.ps1 -TargetUserName $(TargetUserName)

    This is how this should be... Just in case anyone has questions - reply. It works....


    Thanks!

    • Marked as answer by Elaine Jing Monday, September 21, 2015 6:07 AM
    Thursday, September 17, 2015 4:35 PM

All replies

  • Well, I have found some info, but no idea how to do it (as it does not work)...:

    https://social.technet.microsoft.com/Forums/windowsserver/en-US/e5d0a7b1-ee5e-463d-8d6f-4ec5123ffece/help-with-ps-script-that-gets-run-when-an-event-id-is-triggered?forum=winserverpowershell

    You can edit some xml and then pass them as parameters but no idea how to complete it... I dont want to do it via timestamps  - I want the powershell script to get information about eventID that triggered it... 

    Thursday, September 17, 2015 2:31 PM
  • Just add the query to the task xml and pass the values by on the command line.

    Search. There are many good examples.

    Get the XML be exporting your event Then edit the file.   Use the "data" from your event XML.  Find that by viewing XML of an event.

    Once you see how the pieces work it is very easy.

    Start by exporting the task xml.


    \_(ツ)_/


    • Edited by jrv Thursday, September 17, 2015 4:05 PM
    Thursday, September 17, 2015 4:04 PM
  • Just in case some one in the future will be looking for this:

    1. Export xml from task

    2. Add this to xml:

    <ValueQueries>
            <Value name='TargetUserName'>Event/EventData/Data[@Name="TargetUserName"]</Value>
        </ValueQueries> 

    3. Add this to the script being run by the task:

    param(
    $TargetUserName
    )

    And do something with $TargetUserName

    4. Add this to the task (parameter box):

    C:\script.ps1 -TargetUserName $(TargetUserName)

    This is how this should be... Just in case anyone has questions - reply. It works....


    Thanks!

    • Marked as answer by Elaine Jing Monday, September 21, 2015 6:07 AM
    Thursday, September 17, 2015 4:35 PM