I am trying to put together a script that searches for users on an RDS host and if found, removes their user folder and deletes their registry key.
I have multiple RDS servers to check so i was going to use a loop to go through an array.
I am not sure what would be the best way to go about this, as in how to loop through on certain conditions etc…
Would i use a foreach loop, do while or do until? below is a outline of what im trying to achieve.
- Select first RDS host in the array
- Search host for username (username variable is input by user running the script)
- If the user is found on the host then attempt to remove user from RDS session and remove folder from C:\Users (if the user is not found then proceed with searching for user SID in registry and delete if found)
- Once folder is removed, search for SID and remove reg key if found
- Check folder and reg key are deleted, if so loop to next RDS host in the array.
For the array, i am using foreach ($RDSServer in $RDSServers)
I’m having issues with nested if statements within the foreach loop.
What way would be best to tackle this? any help/guidance is much appreciated.