本文整理汇总了C#中System.Management.Automation.Language.Token.Where方法的典型用法代码示例。如果您正苦于以下问题:C# Token.Where方法的具体用法?C# Token.Where怎么用?C# Token.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.Language.Token
的用法示例。
在下文中一共展示了Token.Where方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSingleAstRequiredModules
private static List<string> GetSingleAstRequiredModules(Ast ast, Token[] tokens)
{
List<string> modules = new List<string>();
List<string> resources = new List<string>();
var imports = tokens.Where(token =>
String.Compare(token.Text, "Import-DscResource", StringComparison.OrdinalIgnoreCase) == 0);
//
// Create a function with the same name as Import-DscResource keyword and use powershell
// argument function binding to emulate Import-DscResource argument binding.
//
InitialSessionState initialSessionState = InitialSessionState.Create();
SessionStateFunctionEntry importDscResourcefunctionEntry = new SessionStateFunctionEntry(
"Import-DscResource", @"param($Name, $ModuleName)
if ($ModuleName)
{
foreach ($m in $ModuleName) { $global:modules.Add($m) }
} else {
foreach ($n in $Name) { $global:resources.Add($n) }
}
");
initialSessionState.Commands.Add(importDscResourcefunctionEntry);
initialSessionState.LanguageMode = PSLanguageMode.RestrictedLanguage;
var moduleVarEntry = new SessionStateVariableEntry("modules", modules, "");
var resourcesVarEntry = new SessionStateVariableEntry("resources", resources, "");
initialSessionState.Variables.Add(moduleVarEntry);
initialSessionState.Variables.Add(resourcesVarEntry);
using (System.Management.Automation.PowerShell powerShell = System.Management.Automation.PowerShell.Create(initialSessionState))
{
foreach (var import in imports)
{
int startOffset = import.Extent.StartOffset;
var asts = ast.FindAll(a => IsCandidateForImportDscResourceAst(a, startOffset), true);
int longestLen = -1;
Ast longestCandidate = null;
foreach (var candidatAst in asts)
{
int curLen = candidatAst.Extent.EndOffset - candidatAst.Extent.StartOffset;
if (curLen > longestLen)
{
longestCandidate = candidatAst;
longestLen = curLen;
}
}
// longestCandidate should contain AST for import-dscresource, like "Import-DSCResource -Module x -Name y".
if (longestCandidate != null)
{
string importText = longestCandidate.Extent.Text;
// We invoke-command "importText" here. Script injection is prevented:
// We checked that file represents a valid AST without errors.
powerShell.AddScript(importText);
powerShell.Invoke();
powerShell.Commands.Clear();
}
}
}
modules.AddRange(resources.Select(GetModuleNameForDscResource));
return modules;
}