本文整理汇总了C#中Cmdlet.ThrowTerminatingError方法的典型用法代码示例。如果您正苦于以下问题:C# Cmdlet.ThrowTerminatingError方法的具体用法?C# Cmdlet.ThrowTerminatingError怎么用?C# Cmdlet.ThrowTerminatingError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cmdlet
的用法示例。
在下文中一共展示了Cmdlet.ThrowTerminatingError方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WarnAboutUnsupportedActionPreferences
private static void WarnAboutUnsupportedActionPreferences(
Cmdlet cmdlet,
ActionPreference effectiveActionPreference,
string nameOfCommandLineParameter,
Func<string> inquireMessageGetter,
Func<string> stopMessageGetter)
{
string message;
switch (effectiveActionPreference)
{
case ActionPreference.Stop:
message = stopMessageGetter();
break;
case ActionPreference.Inquire:
message = inquireMessageGetter();
break;
default:
return; // we can handle everything that is not Stop or Inquire
}
bool actionPreferenceComesFromCommandLineParameter = cmdlet.MyInvocation.BoundParameters.ContainsKey(nameOfCommandLineParameter);
if (actionPreferenceComesFromCommandLineParameter)
{
Exception exception = new ArgumentException(message);
ErrorRecord errorRecord = new ErrorRecord(exception, "ActionPreferenceNotSupportedByCimCmdletAdapter", ErrorCategory.NotImplemented, null);
cmdlet.ThrowTerminatingError(errorRecord);
}
}
示例2: Convert
internal static Encoding Convert(Cmdlet cmdlet, string encoding)
{
if (string.IsNullOrEmpty(encoding)
|| (string.Equals(encoding, "unknown", StringComparison.OrdinalIgnoreCase)
|| string.Equals(encoding, "string", StringComparison.OrdinalIgnoreCase))
|| string.Equals(encoding, "unicode", StringComparison.OrdinalIgnoreCase))
{
return Encoding.Unicode;
}
if (string.Equals(encoding, "bigendianunicode", StringComparison.OrdinalIgnoreCase))
{
return Encoding.BigEndianUnicode;
}
if (string.Equals(encoding, "ascii", StringComparison.OrdinalIgnoreCase))
{
return Encoding.ASCII;
}
if (string.Equals(encoding, "utf8", StringComparison.OrdinalIgnoreCase))
{
return Encoding.UTF8;
}
if (string.Equals(encoding, "utf7", StringComparison.OrdinalIgnoreCase))
{
return Encoding.UTF7;
}
if (string.Equals(encoding, "utf32", StringComparison.OrdinalIgnoreCase))
{
return Encoding.UTF32;
}
if (string.Equals(encoding, "default", StringComparison.OrdinalIgnoreCase))
{
return Encoding.Default;
}
if (string.Equals(encoding, "oem", StringComparison.OrdinalIgnoreCase))
{
return Encoding.GetEncoding((int) NativeMethods.GetOEMCP());
}
var str = string.Join(
", ",
"unknown",
"string",
"unicode",
"bigendianunicode",
"ascii",
"utf8",
"utf7",
"utf32",
"default",
"oem");
var message = StringUtil.Format(PathUtilStrings.OutFile_WriteToFileEncodingUnknown, encoding, str);
cmdlet.ThrowTerminatingError(
new ErrorRecord(
ExceptionUtils.NewArgumentException("Encoding"),
"WriteToFileEncodingUnknown",
ErrorCategory.InvalidArgument,
null) {ErrorDetails = new ErrorDetails(message)});
return null;
}
示例3: Convert
internal static Encoding Convert(Cmdlet cmdlet, string encoding)
{
if ((encoding == null) || (encoding.Length == 0))
{
return Encoding.Unicode;
}
if (string.Equals(encoding, "unknown", StringComparison.OrdinalIgnoreCase))
{
return Encoding.Unicode;
}
if (string.Equals(encoding, "string", StringComparison.OrdinalIgnoreCase))
{
return Encoding.Unicode;
}
if (string.Equals(encoding, "unicode", StringComparison.OrdinalIgnoreCase))
{
return Encoding.Unicode;
}
if (string.Equals(encoding, "bigendianunicode", StringComparison.OrdinalIgnoreCase))
{
return Encoding.BigEndianUnicode;
}
if (string.Equals(encoding, "ascii", StringComparison.OrdinalIgnoreCase))
{
return Encoding.ASCII;
}
if (string.Equals(encoding, "utf8", StringComparison.OrdinalIgnoreCase))
{
return Encoding.UTF8;
}
if (string.Equals(encoding, "utf7", StringComparison.OrdinalIgnoreCase))
{
return Encoding.UTF7;
}
if (string.Equals(encoding, "utf32", StringComparison.OrdinalIgnoreCase))
{
return Encoding.UTF32;
}
if (string.Equals(encoding, "default", StringComparison.OrdinalIgnoreCase))
{
return Encoding.Default;
}
if (string.Equals(encoding, "oem", StringComparison.OrdinalIgnoreCase))
{
return Encoding.GetEncoding((int) NativeMethods.GetOEMCP());
}
string str = string.Join(", ", new string[] { "unknown", "string", "unicode", "bigendianunicode", "ascii", "utf8", "utf7", "utf32", "default", "oem" });
string message = StringUtil.Format(PathUtilsStrings.OutFile_WriteToFileEncodingUnknown, encoding, str);
ErrorRecord errorRecord = new ErrorRecord(PSTraceSource.NewArgumentException("Encoding"), "WriteToFileEncodingUnknown", ErrorCategory.InvalidArgument, null) {
ErrorDetails = new ErrorDetails(message)
};
cmdlet.ThrowTerminatingError(errorRecord);
return null;
}
示例4: Handle
/// <summary>
/// Output a message in the appropriate way in a powershell command
/// </summary>
/// <param name="caller">the Cmdlet object running</param>
/// <param name="e">the event containing the message</param>
public static void Handle(Cmdlet caller, MessageEventArgs e)
{
switch (e.MessageType)
{
case MessageType.Verbose:
caller.WriteVerbose(e.Message);
break;
case MessageType.Progress:
caller.WriteProgress(new ProgressRecord(0, e.Activity, e.Message));
break;
case MessageType.Error:
ToolsHelper.WriteException(caller, e.Exception);
caller.ThrowTerminatingError(new ErrorRecord(e.Exception, "0", ErrorCategory.InvalidOperation, caller));
break;
case MessageType.Output:
caller.WriteObject(e.Message);
break;
}
}
示例5: VerifyParameterForbiddenInRemoteRunspace
internal static void VerifyParameterForbiddenInRemoteRunspace(Cmdlet cmdlet, string parameterName)
{
if (NativeCommandProcessor.IsServerSide)
{
Exception exception = new InvalidOperationException(StringUtil.Format(CommandBaseStrings.ParameterNotValidInRemoteRunspace, cmdlet.MyInvocation.InvocationName, parameterName));
ErrorRecord errorRecord = new ErrorRecord(exception, "ParameterNotValidInRemoteRunspace", ErrorCategory.InvalidArgument, null);
cmdlet.ThrowTerminatingError(errorRecord);
}
}
示例6: ReportWildcardingFailure
internal static void ReportWildcardingFailure(Cmdlet cmdlet, string filePath)
{
string message = StringUtil.Format(PathUtilsStrings.OutFile_DidNotResolveFile, filePath);
ErrorRecord errorRecord = new ErrorRecord(new FileNotFoundException(), "FileOpenFailure", ErrorCategory.OpenError, filePath) {
ErrorDetails = new ErrorDetails(message)
};
cmdlet.ThrowTerminatingError(errorRecord);
}
示例7: ReportWrongProviderType
internal static void ReportWrongProviderType(Cmdlet cmdlet, string providerId)
{
string message = StringUtil.Format(PathUtilsStrings.OutFile_ReadWriteFileNotFileSystemProvider, providerId);
ErrorRecord errorRecord = new ErrorRecord(PSTraceSource.NewInvalidOperationException(), "ReadWriteFileNotFileSystemProvider", ErrorCategory.InvalidArgument, null) {
ErrorDetails = new ErrorDetails(message)
};
cmdlet.ThrowTerminatingError(errorRecord);
}
示例8: ReportMultipleFilesNotSupported
internal static void ReportMultipleFilesNotSupported(Cmdlet cmdlet)
{
string message = StringUtil.Format(PathUtilsStrings.OutFile_MultipleFilesNotSupported, new object[0]);
ErrorRecord errorRecord = new ErrorRecord(PSTraceSource.NewInvalidOperationException(), "ReadWriteMultipleFilesNotSupported", ErrorCategory.InvalidArgument, null) {
ErrorDetails = new ErrorDetails(message)
};
cmdlet.ThrowTerminatingError(errorRecord);
}
示例9: ReportFileOpenFailure
internal static void ReportFileOpenFailure(Cmdlet cmdlet, string filePath, Exception e)
{
ErrorRecord errorRecord = new ErrorRecord(e, "FileOpenFailure", ErrorCategory.OpenError, null);
cmdlet.ThrowTerminatingError(errorRecord);
}
示例10: WarnAboutUnsupportedActionPreferences
private static void WarnAboutUnsupportedActionPreferences(Cmdlet cmdlet, ActionPreference effectiveActionPreference, string nameOfCommandLineParameter, Func<string> inquireMessageGetter, Func<string> stopMessageGetter)
{
string str;
ActionPreference actionPreference = effectiveActionPreference;
if (actionPreference == ActionPreference.Stop)
{
str = stopMessageGetter();
}
else if (actionPreference == ActionPreference.Continue)
{
return;
}
else if (actionPreference == ActionPreference.Inquire)
{
str = inquireMessageGetter();
}
else
{
return;
}
bool flag = cmdlet.MyInvocation.BoundParameters.ContainsKey(nameOfCommandLineParameter);
if (flag)
{
Exception argumentException = new ArgumentException(str);
ErrorRecord errorRecord = new ErrorRecord(argumentException, "ActionPreferenceNotSupportedByCimCmdletAdapter", ErrorCategory.NotImplemented, null);
cmdlet.ThrowTerminatingError(errorRecord);
}
}
示例11: Convert
/// <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 Unicode (OS preferred)
return System.Text.Encoding.Unicode;
}
// Default to unicode (this matches Get-Content)
if (string.Equals(encoding, Unknown, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.Unicode;
if (string.Equals(encoding, String, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.Unicode;
// these are the encodings the CLR supports
if (string.Equals(encoding, Unicode, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.Unicode;
if (string.Equals(encoding, BigEndianUnicode, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.BigEndianUnicode;
if (string.Equals(encoding, Utf8, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.UTF8;
if (string.Equals(encoding, Ascii, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.ASCII;
if (string.Equals(encoding, Utf7, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.UTF7;
if (string.Equals(encoding, Utf32, StringComparison.OrdinalIgnoreCase))
return System.Text.Encoding.UTF32;
if (string.Equals(encoding, Default, StringComparison.OrdinalIgnoreCase))
return ClrFacade.GetDefaultEncoding();
if (string.Equals(encoding, OEM, StringComparison.OrdinalIgnoreCase))
{
return ClrFacade.GetOEMEncoding();
}
// error condition: unknown encoding value
string validEncodingValues = string.Join(
", ",
new string[] { Unknown, String, Unicode, BigEndianUnicode, Ascii, Utf8, Utf7, Utf32, Default, OEM });
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;
}
示例12: ImportJsonDotNetModule
public static void ImportJsonDotNetModule(Cmdlet cmdlet)
{
const string jsonDotNetAssemblyName = "Newtonsoft.Json, Version=7.0.0.0";
// Check if the Newtonsoft.Json.dll assembly is loaded.
try
{
System.Reflection.Assembly.Load(new AssemblyName(jsonDotNetAssemblyName));
}
catch (System.IO.FileNotFoundException)
{
// It is not, try to load it.
// Make sure that PSModuleAutoLoadingPreference is not set to 'None'.
PSModuleAutoLoadingPreference moduleAutoLoadingPreference =
CommandDiscovery.GetCommandDiscoveryPreference(cmdlet.Context, SpecialVariables.PSModuleAutoLoadingPreferenceVarPath, "PSModuleAutoLoadingPreference");
if (moduleAutoLoadingPreference == PSModuleAutoLoadingPreference.None)
{
cmdlet.ThrowTerminatingError(new ErrorRecord(
new NotSupportedException(WebCmdletStrings.PSModuleAutoloadingPreferenceNotEnable),
"PSModuleAutoloadingPreferenceNotEnable",
ErrorCategory.NotEnabled,
null));
}
// Use module auto-loading to import Json.Net.
var jsonNetModulePath = Path.Combine(System.Environment.GetEnvironmentVariable("ProgramFiles"), @"WindowsPowerShell\Modules\Json.Net");
CmdletInfo cmdletInfo = cmdlet.Context.SessionState.InvokeCommand.GetCmdlet("Microsoft.PowerShell.Core\\Import-Module");
Exception exception;
Collection<PSModuleInfo> importedModule = CommandDiscovery.AutoloadSpecifiedModule(jsonNetModulePath, cmdlet.Context, cmdletInfo.Visibility, out exception);
if ((importedModule == null) || (importedModule.Count == 0))
{
string errorMessage = StringUtil.Format(WebCmdletStrings.JsonNetModuleRequired, WebCmdletStrings.CouldNotAutoImportJsonNetModule);
cmdlet.ThrowTerminatingError(new ErrorRecord(
new NotSupportedException(errorMessage, exception),
"CouldNotAutoImportJsonNetModule",
ErrorCategory.InvalidOperation,
null));
}
// Finally, ensure that the Newtonsoft.Json.dll assembly was loaded.
try
{
System.Reflection.Assembly.Load(new AssemblyName(jsonDotNetAssemblyName));
}
catch (System.IO.FileNotFoundException)
{
string errorMessage = StringUtil.Format(
WebCmdletStrings.JsonNetModuleRequired,
StringUtil.Format(WebCmdletStrings.JsonNetModuleFilesRequired, jsonNetModulePath));
cmdlet.ThrowTerminatingError(new ErrorRecord(
new NotSupportedException(errorMessage),
"JsonNetModuleRequired",
ErrorCategory.NotInstalled,
null));
}
}
}