Distributed Resilient Control of Multi-agent Systems with Applications to Microgrids