本文整理汇总了C#中System.Management.Automation.PSCmdlet.WriteError方法的典型用法代码示例。如果您正苦于以下问题:C# PSCmdlet.WriteError方法的具体用法?C# PSCmdlet.WriteError怎么用?C# PSCmdlet.WriteError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.PSCmdlet
的用法示例。
在下文中一共展示了PSCmdlet.WriteError方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Glob
/// <summary>
///
/// </summary>
/// <param name="files"></param>
/// <param name="errorId"></param>
/// <param name="cmdlet"></param>
/// <returns></returns>
internal static Collection<string> Glob(string[] files, string errorId, PSCmdlet cmdlet)
{
Collection<string> retValue = new Collection<string>();
foreach (string file in files)
{
Collection<string> providerPaths;
ProviderInfo provider = null;
try
{
providerPaths = cmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath(file, out provider);
}
catch (SessionStateException e)
{
cmdlet.WriteError(new ErrorRecord(e, errorId, ErrorCategory.InvalidOperation, file));
continue;
}
if (!provider.NameEquals(cmdlet.Context.ProviderNames.FileSystem))
{
ReportWrongProviderType(provider.FullName, errorId, cmdlet);
continue;
}
foreach (string providerPath in providerPaths)
{
if (!providerPath.EndsWith(".ps1xml", StringComparison.OrdinalIgnoreCase))
{
ReportWrongExtension(providerPath, "WrongExtension", cmdlet);
continue;
}
retValue.Add(providerPath);
}
}
return retValue;
}
示例2: ReportWrongProviderType
private static void ReportWrongProviderType(string providerId, string errorId, PSCmdlet cmdlet)
{
ErrorRecord errorRecord = new ErrorRecord(
PSTraceSource.NewInvalidOperationException(UpdateDataStrings.UpdateData_WrongProviderError, providerId),
errorId,
ErrorCategory.InvalidArgument,
null);
cmdlet.WriteError(errorRecord);
}
示例3: ReportWrongExtension
private static void ReportWrongExtension(string file, string errorId, PSCmdlet cmdlet)
{
ErrorRecord errorRecord = new ErrorRecord(
PSTraceSource.NewInvalidOperationException(UpdateDataStrings.UpdateData_WrongExtension, file, "ps1xml"),
errorId,
ErrorCategory.InvalidArgument,
null);
cmdlet.WriteError(errorRecord);
}
示例4: CreateOrderMatrix
internal static List<OrderByPropertyEntry> CreateOrderMatrix(PSCmdlet cmdlet, List<PSObject> inputObjects, List<MshParameter> mshParameterList)
{
List<OrderByPropertyEntry> list = new List<OrderByPropertyEntry>();
foreach (PSObject obj2 in inputObjects)
{
if ((obj2 != null) && (obj2 != AutomationNull.Value))
{
List<ErrorRecord> errors = new List<ErrorRecord>();
List<string> propertyNotFoundMsgs = new List<string>();
OrderByPropertyEntry item = OrderByPropertyEntryEvaluationHelper.ProcessObject(obj2, mshParameterList, errors, propertyNotFoundMsgs);
foreach (ErrorRecord record in errors)
{
cmdlet.WriteError(record);
}
foreach (string str in propertyNotFoundMsgs)
{
cmdlet.WriteDebug(str);
}
list.Add(item);
}
}
return list;
}
示例5: Glob
internal static Collection<string> Glob(string[] files, string errorId, PSCmdlet cmdlet)
{
Collection<string> collection = new Collection<string>();
foreach (string str in files)
{
Collection<string> resolvedProviderPathFromPSPath;
ProviderInfo provider = null;
try
{
resolvedProviderPathFromPSPath = cmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath(str, out provider);
}
catch (SessionStateException exception)
{
cmdlet.WriteError(new ErrorRecord(exception, errorId, ErrorCategory.InvalidOperation, str));
continue;
}
if (!provider.NameEquals(cmdlet.Context.ProviderNames.FileSystem))
{
ReportWrongProviderType(provider.FullName, errorId, cmdlet);
}
else
{
foreach (string str2 in resolvedProviderPathFromPSPath)
{
if (!str2.EndsWith(".ps1xml", StringComparison.OrdinalIgnoreCase))
{
ReportWrongExtension(str2, "WrongExtension", cmdlet);
}
else
{
collection.Add(str2);
}
}
}
}
return collection;
}
示例6: InvokeWin32ShutdownUsingWsman
/// <summary>
/// Invokes the Win32Shutdown command on provided target computer using WSMan
/// over a CIMSession. The flags parameter determines the type of shutdown operation
/// such as shutdown, reboot, force etc.
/// </summary>
/// <param name="cmdlet">Cmdlet host for reporting errors</param>
/// <param name="isLocalhost">True if local host computer</param>
/// <param name="computerName">Target computer</param>
/// <param name="flags">Win32Shutdown flags</param>
/// <param name="credential">Optional credential</param>
/// <param name="authentication">Optional authentication</param>
/// <param name="formatErrorMessage">Error message format string that takes two parameters</param>
/// <param name="ErrorFQEID">Fully qualified error Id</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>True on success</returns>
internal static bool InvokeWin32ShutdownUsingWsman(
PSCmdlet cmdlet,
bool isLocalhost,
string computerName,
object[] flags,
PSCredential credential,
string authentication,
string formatErrorMessage,
string ErrorFQEID,
CancellationToken cancelToken)
{
Dbg.Diagnostics.Assert(flags.Length == 2, "Caller need to verify the flags passed in");
bool isSuccess = false;
string targetMachine = isLocalhost ? "localhost" : computerName;
string authInUse = isLocalhost ? null : authentication;
PSCredential credInUse = isLocalhost ? null : credential;
var currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE();
var operationOptions = new CimOperationOptions
{
Timeout = TimeSpan.FromMilliseconds(10000),
CancellationToken = cancelToken,
//This prefix works against all versions of the WinRM server stack, both win8 and win7
ResourceUriPrefix = new Uri(ComputerWMIHelper.CimUriPrefix)
};
try
{
if (!(isLocalhost && PlatformInvokes.EnableTokenPrivilege(ComputerWMIHelper.SE_SHUTDOWN_NAME, ref currentPrivilegeState)) &&
!(!isLocalhost && PlatformInvokes.EnableTokenPrivilege(ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME, ref currentPrivilegeState)))
{
string message =
StringUtil.Format(ComputerResources.PrivilegeNotEnabled, computerName,
isLocalhost ? ComputerWMIHelper.SE_SHUTDOWN_NAME : ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME);
ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(message), "PrivilegeNotEnabled", ErrorCategory.InvalidOperation, null);
cmdlet.WriteError(errorRecord);
return false;
}
using (CimSession cimSession = RemoteDiscoveryHelper.CreateCimSession(targetMachine, credInUse, authInUse, cancelToken, cmdlet))
{
var methodParameters = new CimMethodParametersCollection();
methodParameters.Add(CimMethodParameter.Create(
"Flags",
flags[0],
Microsoft.Management.Infrastructure.CimType.SInt32,
CimFlags.None));
methodParameters.Add(CimMethodParameter.Create(
"Reserved",
flags[1],
Microsoft.Management.Infrastructure.CimType.SInt32,
CimFlags.None));
CimMethodResult result = cimSession.InvokeMethod(
ComputerWMIHelper.CimOperatingSystemNamespace,
ComputerWMIHelper.WMI_Class_OperatingSystem,
ComputerWMIHelper.CimOperatingSystemShutdownMethod,
methodParameters,
operationOptions);
int retVal = Convert.ToInt32(result.ReturnValue.Value, CultureInfo.CurrentCulture);
if (retVal != 0)
{
var ex = new Win32Exception(retVal);
string errMsg = StringUtil.Format(formatErrorMessage, computerName, ex.Message);
ErrorRecord error = new ErrorRecord(
new InvalidOperationException(errMsg), ErrorFQEID, ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(error);
}
else
{
isSuccess = true;
}
}
}
catch (CimException ex)
{
string errMsg = StringUtil.Format(formatErrorMessage, computerName, ex.Message);
ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), ErrorFQEID,
ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(error);
}
catch (Exception ex)
{
//.........这里部分代码省略.........
示例7: SkipSystemRestoreOperationForARMPlatform
/// <summary>
/// System Restore APIs are not supported on the ARM platform. Skip the system restore operation is necessary.
/// </summary>
/// <param name="cmdlet"></param>
/// <returns></returns>
internal static bool SkipSystemRestoreOperationForARMPlatform(PSCmdlet cmdlet)
{
bool retValue = false;
if (PsUtils.IsRunningOnProcessorArchitectureARM())
{
var ex = new InvalidOperationException(ComputerResources.SystemRestoreNotSupported);
var er = new ErrorRecord(ex, "SystemRestoreNotSupported", ErrorCategory.InvalidOperation, null);
cmdlet.WriteError(er);
retValue = true;
}
return retValue;
}
示例8: WriteNonTerminatingError
internal static void WriteNonTerminatingError(int errorcode, PSCmdlet cmdlet, string computername)
{
Win32Exception ex = new Win32Exception(errorcode);
string additionalmessage = String.Empty;
if (ex.NativeErrorCode.Equals(0x00000035))
{
additionalmessage = StringUtil.Format(ComputerResources.NetworkPathNotFound, computername);
}
string message = StringUtil.Format(ComputerResources.OperationFailed, ex.Message, computername, additionalmessage);
ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, computername);
cmdlet.WriteError(er);
}
示例9: RestartOneComputerUsingDcom
/// <summary>
/// Restart one computer
/// </summary>
/// <param name="cmdlet"></param>
/// <param name="isLocalhost"></param>
/// <param name="computerName"></param>
/// <param name="flags"></param>
/// <param name="options"></param>
/// <returns>
/// True if the restart was successful
/// False otherwise
/// </returns>
internal static bool RestartOneComputerUsingDcom(PSCmdlet cmdlet, bool isLocalhost, string computerName, object[] flags, ConnectionOptions options)
{
bool isSuccess = false;
PlatformInvokes.TOKEN_PRIVILEGE currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE();
try
{
if (!(isLocalhost && PlatformInvokes.EnableTokenPrivilege(ComputerWMIHelper.SE_SHUTDOWN_NAME, ref currentPrivilegeState)) &&
!(!isLocalhost && PlatformInvokes.EnableTokenPrivilege(ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME, ref currentPrivilegeState)))
{
string message =
StringUtil.Format(ComputerResources.PrivilegeNotEnabled, computerName,
isLocalhost ? ComputerWMIHelper.SE_SHUTDOWN_NAME : ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME);
ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(message), "PrivilegeNotEnabled", ErrorCategory.InvalidOperation, null);
cmdlet.WriteError(errorRecord);
return false;
}
ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(isLocalhost ? "localhost" : computerName, ComputerWMIHelper.WMI_Path_CIM), options);
EnumerationOptions enumOptions = new EnumerationOptions { UseAmendedQualifiers = true, DirectRead = true };
ObjectQuery query = new ObjectQuery("select * from " + ComputerWMIHelper.WMI_Class_OperatingSystem);
using (var searcher = new ManagementObjectSearcher(scope, query, enumOptions))
{
foreach (ManagementObject operatingSystem in searcher.Get())
{
using (operatingSystem)
{
object result = operatingSystem.InvokeMethod("Win32shutdown", flags);
int retVal = Convert.ToInt32(result.ToString(), CultureInfo.CurrentCulture);
if (retVal != 0)
{
var ex = new Win32Exception(retVal);
string errMsg = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, ex.Message);
ErrorRecord error = new ErrorRecord(
new InvalidOperationException(errMsg), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(error);
}
else
{
isSuccess = true;
}
}
}
}
}
catch (ManagementException ex)
{
string errMsg = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, ex.Message);
ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "RestartcomputerFailed",
ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(error);
}
catch (COMException ex)
{
string errMsg = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, ex.Message);
ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "RestartcomputerFailed",
ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(error);
}
catch (UnauthorizedAccessException ex)
{
string errMsg = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, ex.Message);
ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "RestartcomputerFailed",
ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(error);
}
finally
{
// Restore the previous privilege state if something unexpected happened
PlatformInvokes.RestoreTokenPrivilege(
isLocalhost ? ComputerWMIHelper.SE_SHUTDOWN_NAME : ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME, ref currentPrivilegeState);
}
return isSuccess;
}
示例10: WriteNonTerminatingError
internal static void WriteNonTerminatingError(int errorcode, PSCmdlet cmdlet, string computername)
{
Win32Exception win32Exception = new Win32Exception(errorcode);
string empty = string.Empty;
int nativeErrorCode = win32Exception.NativeErrorCode;
if (nativeErrorCode.Equals(53))
{
empty = StringUtil.Format(ComputerResources.NetworkPathNotFound, computername);
}
object[] message = new object[3];
message[0] = win32Exception.Message;
message[1] = computername;
message[2] = empty;
string str = StringUtil.Format(ComputerResources.OperationFailed, message);
ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, computername);
cmdlet.WriteError(errorRecord);
}
示例11: SkipSystemRestoreOperationForARMPlatform
internal static bool SkipSystemRestoreOperationForARMPlatform(PSCmdlet cmdlet)
{
bool flag = false;
if (PsUtils.IsRunningOnProcessorArchitectureARM())
{
InvalidOperationException invalidOperationException = new InvalidOperationException(ComputerResources.SystemRestoreNotSupported);
ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "SystemRestoreNotSupported", ErrorCategory.InvalidOperation, null);
cmdlet.WriteError(errorRecord);
flag = true;
}
return flag;
}
示例12: RestartOneComputerUsingWsman
internal static bool RestartOneComputerUsingWsman(PSCmdlet cmdlet, bool isLocalhost, string computerName, object[] flags, PSCredential credential, string authentication, CancellationToken token)
{
bool flag;
string str;
string str1;
PSCredential pSCredential;
object obj;
string str2;
bool flag1 = false;
if (isLocalhost)
{
str = "localhost";
}
else
{
str = computerName;
}
string str3 = str;
if (isLocalhost)
{
str1 = null;
}
else
{
str1 = authentication;
}
string str4 = str1;
if (isLocalhost)
{
pSCredential = null;
}
else
{
pSCredential = credential;
}
PSCredential pSCredential1 = pSCredential;
Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
CimOperationOptions cimOperationOption = new CimOperationOptions();
cimOperationOption.Timeout = TimeSpan.FromMilliseconds(10000);
cimOperationOption.CancellationToken = new CancellationToken?(token);
CimOperationOptions cimOperationOption1 = cimOperationOption;
try
{
try
{
if ((!isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE)) && (isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE)))
{
string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
string str5 = computerName;
if (isLocalhost)
{
obj = "SeShutdownPrivilege";
}
else
{
obj = "SeRemoteShutdownPrivilege";
}
string str6 = StringUtil.Format(privilegeNotEnabled, str5, obj);
ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str6), "PrivilegeNotEnabled", ErrorCategory.InvalidOperation, null);
cmdlet.WriteError(errorRecord);
flag = false;
return flag;
}
else
{
CimSession cimSession = RemoteDiscoveryHelper.CreateCimSession(str3, pSCredential1, str4, token, cmdlet);
using (cimSession)
{
CimMethodParametersCollection cimMethodParametersCollection = new CimMethodParametersCollection();
cimMethodParametersCollection.Add(CimMethodParameter.Create("Flags", flags[0], Microsoft.Management.Infrastructure.CimType.SInt32, (CimFlags)((long)0)));
cimMethodParametersCollection.Add(CimMethodParameter.Create("Reserved", flags[1], Microsoft.Management.Infrastructure.CimType.SInt32, (CimFlags)((long)0)));
CimMethodResult cimMethodResult = cimSession.InvokeMethod("root/cimv2", "Win32_OperatingSystem", "Win32shutdown", cimMethodParametersCollection, cimOperationOption1);
int num = Convert.ToInt32(cimMethodResult.ReturnValue.Value, CultureInfo.CurrentCulture);
if (num == 0)
{
flag1 = true;
}
else
{
Win32Exception win32Exception = new Win32Exception(num);
string str7 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, win32Exception.Message);
ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str7), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(errorRecord1);
}
}
}
}
catch (CimException cimException1)
{
CimException cimException = cimException1;
string str8 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, cimException.Message);
ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str8), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(errorRecord2);
}
catch (Exception exception1)
{
Exception exception = exception1;
CommandProcessorBase.CheckForSevereException(exception);
string str9 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, exception.Message);
ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str9), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
//.........这里部分代码省略.........
示例13: RestartOneComputerUsingDcom
internal static bool RestartOneComputerUsingDcom(PSCmdlet cmdlet, bool isLocalhost, string computerName, object[] flags, ConnectionOptions options)
{
bool flag;
object obj;
string str;
string str1;
bool flag1 = false;
ManagementObjectSearcher managementObjectSearcher = null;
Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
try
{
try
{
if ((!isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE)) && (isLocalhost || !ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE)))
{
string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
string str2 = computerName;
if (isLocalhost)
{
obj = "SeShutdownPrivilege";
}
else
{
obj = "SeRemoteShutdownPrivilege";
}
string str3 = StringUtil.Format(privilegeNotEnabled, str2, obj);
ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str3), "PrivilegeNotEnabled", ErrorCategory.InvalidOperation, null);
cmdlet.WriteError(errorRecord);
flag = false;
return flag;
}
else
{
if (isLocalhost)
{
str = "localhost";
}
else
{
str = computerName;
}
ManagementScope managementScope = new ManagementScope(ComputerWMIHelper.GetScopeString(str, "\\root\\cimv2"), options);
EnumerationOptions enumerationOption = new EnumerationOptions();
enumerationOption.UseAmendedQualifiers = true;
enumerationOption.DirectRead = true;
EnumerationOptions enumerationOption1 = enumerationOption;
ObjectQuery objectQuery = new ObjectQuery("select * from Win32_OperatingSystem");
managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption1);
foreach (ManagementObject managementObject in managementObjectSearcher.Get())
{
object obj1 = managementObject.InvokeMethod("Win32shutdown", flags);
int num = Convert.ToInt32(obj1.ToString(), CultureInfo.CurrentCulture);
if (num == 0)
{
flag1 = true;
}
else
{
Win32Exception win32Exception = new Win32Exception(num);
string str4 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, win32Exception.Message);
ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str4), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(errorRecord1);
}
}
}
}
catch (ManagementException managementException1)
{
ManagementException managementException = managementException1;
string str5 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, managementException.Message);
ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str5), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(errorRecord2);
}
catch (COMException cOMException1)
{
COMException cOMException = cOMException1;
string str6 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, cOMException.Message);
ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str6), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(errorRecord3);
}
catch (UnauthorizedAccessException unauthorizedAccessException1)
{
UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
string str7 = StringUtil.Format(ComputerResources.RestartcomputerFailed, computerName, unauthorizedAccessException.Message);
ErrorRecord errorRecord4 = new ErrorRecord(new InvalidOperationException(str7), "RestartcomputerFailed", ErrorCategory.OperationStopped, computerName);
cmdlet.WriteError(errorRecord4);
}
return flag1;
}
finally
{
if (isLocalhost)
{
str1 = "SeShutdownPrivilege";
}
else
{
str1 = "SeRemoteShutdownPrivilege";
}
ComputerWMIHelper.RestoreTokenPrivilege(str1, ref tOKENPRIVILEGE);
//.........这里部分代码省略.........