A man who exposed himself outside a stranger's window and performed a sex act, knowing she could see him, has been given a suspended prison sentence.