How to generate the custom event from script.

Welcome Forums General PowerShell Q&A How to generate the custom event from script.

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #167461

    Participant
    Topics: 8
    Replies: 8
    Points: 90
    Rank: Member

    Hi, there!

    I have read the documentation about PowerShell events cmndlets (New-Event, New-ObjectEvent, Register-EngineEvent, etc.), but there no clear information about this kind of task: for example I need to raise the event (to write "It's reached"), when some code is reach some value:

    for($i=0; $i -gt 10; $i++) {

    if($i -eq 5) { #HERE THE CODE FOR GENERATE EVENT – ?????? }

    }

    I couldn't find the similar code example through the Google, so may somebody to provide the code example for generate of this type event ?

     

  • #167566

    Participant
    Topics: 2
    Replies: 54
    Points: 277
    Helping Hand
    Rank: Contributor

    Ah, speaking of clear information, perhaps you could give a bit more detail on what you want to do.

    If you just want the script to write "It's reached" to the screen when a condition is met, it's as simple as this:

    if ($i -eq 5) {echo "It's reached"}

    and in fact you don't even need 'echo', so it can just be:

    if ($i -eq 5) {"It's reached"}

    Why do you need an event, what is the event supposed to trigger, and what is handling the event when it occurs?

    Also, please format your code with pre tags or by using a github gist, as per this post.

  • #167788

    Participant
    Topics: 8
    Replies: 8
    Points: 90
    Rank: Member

    Hello!

    You're right.

    Perhaps it's not clear task and I don't need to use events here – sorry. I'll try to think better on this task.

    Thanks.

You must be logged in to reply to this topic.