I've seen some doubts around about how useful Salt would be for desktop administration, so I thought I would share my Salt grain that makes things easier for me. It gives you a list of users that have profiles on that minion. So the end result is that you can do this in your states:
{% for usr in grains['present_users'] %}
C:\Users\{{ usr }}\AppData\Local\Something:
file.directory:
- makedirs: True
{% endfor %}
This has only been tested on Salt 2017.7.1, and it will definitely break with the next major release because they are changing some things that it relies on.
To use this custom grain, create a _grains folder within your salt states folder/repo, then save this as a file there:
presentusersgrain.py
import socket
import salt.utils
if salt.utils.is_windows():
import salt.utils.winapi
import wmi
def presentusers():
if salt.utils.is_windows():
with salt.utils.winapi.Com():
wmi_c = wmi.WMI()
userprofiles = [x.LocalPath.split('\\')
for x in wmi_c.Win32_UserProfile()]
sysprof = ['systemprofile', 'ServiceProfiles']
userlist = [x[-1] for x in userprofiles
if not any(word in x for word in sysprof)]
return {'present_users': userlist}
return {'present_users': 'n/a'}
Then run salt '*' saltutil.sync_grains
to sync the custom grain to your minions. Now run salt '*' grains.items
to see your new grain