Passing Variables Into Statement Block Brackets In PowerShell -


i have array want pass variable passed register-objectevent.

it looks following:

$body = @{     project_id = $project_id     task_token = $task_token }  $action = {invoke-restmethod -uri http://localhost/temp.php -method post -body $body}  register-objectevent -inputobject $timer -eventname elapsed -sourceidentifier mytimer -action $action 

i noticed in above $project_id , $task_token variables lost when placed between {} when assigning script block $action.

how preserve variables here?

your code not supposed work in way write @ least 2 reasons.

first : not sure one, when write :

$body = @{     project_id = $project_id     task_token = $task_token } 

$body hashtable sure constitute value -body parameter ?

second : action script block going executed in job $body variable not available in scope of action block writen in register-objectevent documentation :"the value of action parameter can include $event, $eventsubscriber, $sender, $eventargs, , $args automatic variables, provide information event action script block. more information, see about_automatic_variables.".

so in case, if want specifie additional data associated event subscription, can use -messagedata parameter way (here don' take care of first remark):

$action = {invoke-restmethod -uri http://localhost/temp.php -method post -body $($event.messagedata)"} register-objectevent -inputobject $timer -eventname elapsed -sourceidentifier mytimer -action $action -messagedata $body 

Comments