private static ManagementScope getScope(string target, bool subscription) { ManagementScope scope; if (subscription) { scope = new ManagementScope("\\\\" + target + "\\Root\\subscription", options); } else { scope = new ManagementScope("\\\\" + target + "\\Root\\CIMV2", options); } try { scope.Connect(); } //user credentials cannot be used in local connections catch (ManagementException) { scope.Options = new ConnectionOptions(); scope.Connect(); } catch (UnauthorizedAccessException) { //scope refers to local ip. If credential in options a different from current user credentials, //it will cause an 'access denied' exception if (IPHandler.get_local_ip().Equals(target) || target.Equals("127.0.0.1")) { scope.Options = new ConnectionOptions(); scope.Connect(); } else { //access denied (wrong credentials) return(null); } } //RPC server is unavaliable catch (System.Runtime.InteropServices.COMException e) { return(null); } return(scope); }