本文整理汇总了C#中System.Management.Automation.ScriptBlock.GetPowerShell方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptBlock.GetPowerShell方法的具体用法?C# ScriptBlock.GetPowerShell怎么用?C# ScriptBlock.GetPowerShell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.ScriptBlock
的用法示例。
在下文中一共展示了ScriptBlock.GetPowerShell方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateCompatibleLanguageMode
/// <summary>
/// Ensures that the provided script block is compatible with the current language mode - to
/// be used when a script block is being dotted.
/// </summary>
/// <param name="scriptBlock">The script block being dotted</param>
/// <param name="languageMode">The current language mode</param>
/// <param name="invocationInfo">The invocation info about the command</param>
protected static void ValidateCompatibleLanguageMode(ScriptBlock scriptBlock,
PSLanguageMode languageMode,
InvocationInfo invocationInfo)
{
// If we are in a constrained language mode (Core or Restricted), block it.
// We are currently restricting in one direction:
// - Can't dot something from a more permissive mode, since that would probably expose
// functions that were never designed to handle untrusted data.
// This function won't be called for NoLanguage mode so the only direction checked is trusted
// (FullLanguage mode) script running in a constrained/restricted session.
if ((scriptBlock.LanguageMode.HasValue) &&
(scriptBlock.LanguageMode != languageMode) &&
((languageMode == PSLanguageMode.RestrictedLanguage) ||
(languageMode == PSLanguageMode.ConstrainedLanguage)))
{
// Finally check if script block is really just PowerShell commands plus parameters.
// If so then it is safe to dot source across language mode boundaries.
bool isSafeToDotSource = false;
try
{
scriptBlock.GetPowerShell();
isSafeToDotSource = true;
}
catch (Exception e)
{
CheckForSevereException(e);
}
if (!isSafeToDotSource)
{
ErrorRecord errorRecord = new ErrorRecord(
new NotSupportedException(
DiscoveryExceptions.DotSourceNotSupported),
"DotSourceNotSupported",
ErrorCategory.InvalidOperation,
null);
errorRecord.SetInvocationInfo(invocationInfo);
throw new CmdletInvocationException(errorRecord);
}
}
}