Now that the HassAgent Command is set up we head back to HA and create the automation
First I use to validate the template I will use in my automation. And for this scenario I would like to extract the two values for input_number.logonhours_start and input_number.logonhours_stop. I do not want any decimals so I will try
{{ states('input_number.logonhours_start') | round}} {{ states('input_number.logonhours_stop') | round}}
And if I go to validate the template it looks like this
Right now my starttime is set to 0800 and my stoptime is set to 1700 (you can only set hours in Active Directory LogonHours, that’s why ignored the minutes)
Head over to Settings and then Automations and Scenes
Create a new automation

And add a trigger for state
Select the entities for both LogonHours Start and LogonHours Stop
Next we create an action for call service and select MQTT: Publish
And here we need that MQTT Action Topic we looked at when we created the HassAgent Command
Past in the MQTT Action Topic from HassAgent Command in to Topic
Select Payload and the we past in the path to the PowerShell script on you Domain Controller and the template we tested earlier
Would look something like
" \"c:\\LogonHours\\LogonHours.ps1 {{ states('input_number.logonhours_start') | round}} {{ states('input_number.logonhours_stop') | round}} \""
Note that we need to use Escaped character for " and backslash, thats why the path to PowerShell script looks a bit odd.
When you past in the payload the webpage change to YAML Edit mode as it can not handle this in GUI
Save your automation and you should be all done.
If you now move the sliders for LogonHours in your dashboard your automation should change the LogonOurs for User1 in you Active Directory. Be aware that you will have to refresh the Active Directory Users and Computers GUI to see the changes.
You could extend this to use a dynamic user or set the LogonHours for a couple of days instead of only for today.
As this are for my kids I have an automation that runs every day in the middel of the night to set the todays value for LogonHours Start and LogonHours Stop. Depending if it’s school day or not the values are different. This way theire LogonHours in ActiveDirectory is only set for today, and I will rename my kids to User1 and User2 