private static void WriteErrorOrWarning(bool writeErrorOnException, Cmdlet cmdlet, Exception exception, string identifier, JobSourceAdapter sourceAdapter) { try { if (writeErrorOnException) { cmdlet.WriteError(new ErrorRecord(exception, identifier, ErrorCategory.OpenError, sourceAdapter)); } else { // Write a warning string message = string.Format(CultureInfo.CurrentCulture, RemotingErrorIdStrings.JobSourceAdapterError, exception.Message, sourceAdapter.Name); cmdlet.WriteWarning(message); } } catch (Exception) { // if this call is not made from a cmdlet thread or if // the cmdlet is closed this will thrown an exception // it is fine to eat that exception } }
} // WriteVerbose /// <summary> /// Writes the object to the Warning pipe. /// </summary> /// <param name="text"> /// The string that needs to be written. /// </param> internal void WriteWarning(string text) { if (_command != null) { _command.WriteWarning(text); } } // WriteWarning
/// <summary> /// Warn if the encoding has been designated as obsolete. /// </summary> /// <param name="cmdlet">A cmdlet instance which is used to emit the warning.</param> /// <param name="encoding">The encoding to check for obsolescence.</param> internal static void WarnIfObsolete(Cmdlet cmdlet, Encoding encoding) { if (encoding == System.Text.Encoding.UTF7) { cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete); } }
private static void WriteMessage(System.Management.Automation.Cmdlet cmdlet, string message) { if (!IsMessageSuppressed) { cmdlet.WriteWarning(message); } }
/// <summary> /// Warn if the encoding has been designated as obsolete. /// </summary> /// <param name="cmdlet">A cmdlet instance which is used to emit the warning.</param> /// <param name="encoding">The encoding to check for obsolescence.</param> internal static void WarnIfObsolete(Cmdlet cmdlet, Encoding encoding) { // Check for UTF-7 by checking for code page 65000 // See: https://docs.microsoft.com/en-us/dotnet/core/compatibility/corefx#utf-7-code-paths-are-obsolete if (encoding != null && encoding.CodePage == 65000) { cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete); } }
/// <summary> /// Warn if the encoding has been designated as obsolete. /// </summary> /// <param name="cmdlet">A cmdlet instance which is used to emit the warning.</param> /// <param name="encoding">The encoding to check for obsolescence.</param> internal static void WarnIfObsolete(Cmdlet cmdlet, Encoding encoding) { #pragma warning disable 612, 618 if (encoding == System.Text.Encoding.UTF7) #pragma warning restore 612, 618 { cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete); } }
private static void WriteErrorOrWarning(bool writeErrorOnException, Cmdlet cmdlet, Exception exception, string identifier, JobSourceAdapter sourceAdapter) { try { if (writeErrorOnException) { cmdlet.WriteError(new ErrorRecord(exception, identifier, ErrorCategory.OpenError, sourceAdapter)); } else { cmdlet.WriteWarning(ResourceManagerCache.FormatResourceString("RemotingErrorIdStrings", "JobSourceAdapterError", new object[] { exception.Message, sourceAdapter.Name })); } } catch (Exception) { } }
/// <summary> /// Retrieve the encoding parameter from the command line /// it throws if the encoding does not match the known ones. /// </summary> /// <returns>A System.Text.Encoding object (null if no encoding specified).</returns> internal static Encoding Convert(Cmdlet cmdlet, string encoding) { if (string.IsNullOrEmpty(encoding)) { // no parameter passed, default to UTF8 return(ClrFacade.GetDefaultEncoding()); } Encoding foundEncoding; if (encodingMap.TryGetValue(encoding, out foundEncoding)) { // Write a warning if using utf7 as it is obsolete in .NET5 if (string.Compare(encoding, Utf7, StringComparison.OrdinalIgnoreCase) == 0) { cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete); } return(foundEncoding); } // error condition: unknown encoding value string validEncodingValues = string.Join(", ", TabCompletionResults); string msg = StringUtil.Format(PathUtilsStrings.OutFile_WriteToFileEncodingUnknown, encoding, validEncodingValues); ErrorRecord errorRecord = new ErrorRecord( PSTraceSource.NewArgumentException("Encoding"), "WriteToFileEncodingUnknown", ErrorCategory.InvalidArgument, null); errorRecord.ErrorDetails = new ErrorDetails(msg); cmdlet.ThrowTerminatingError(errorRecord); return(null); }