/// <summary> /// Add an new SessionStateVariable entry to this session state object... /// </summary> /// <param name="entry">The entry to add</param> internal void AddSessionStateEntry(SessionStateVariableEntry entry) { PSVariable v = new PSVariable(entry.Name, entry.Value, entry.Options, entry.Attributes, entry.Description); v.Visibility = entry.Visibility; this.SetVariableAtScope(v, "global", true, CommandOrigin.Internal); }
public void AddVariableToSessionState() { InitialSessionState sessionState = InitialSessionState.CreateDefault(); var variableEntry = new SessionStateVariableEntry("TestVariable", new object(), "description"); sessionState.Variables.Add(variableEntry); Assert.IsTrue(sessionState.Variables.Contains(variableEntry)); }
Runspace GetWPFCurrentThreadRunspace(InitialSessionState sessionState) { InitialSessionState clone = sessionState.Clone(); clone.ThreadOptions = PSThreadOptions.UseCurrentThread; SessionStateVariableEntry window = new SessionStateVariableEntry("Window", JobWindow, ""); SessionStateVariableEntry namedControls = new SessionStateVariableEntry("NamedControls", this.namedControls, ""); clone.Variables.Add(window); clone.Variables.Add(namedControls); return RunspaceFactory.CreateRunspace(clone); }
public void NewVariableEntryWithNameValueAndDescriptionShouldHaveOptionsOfNoneAndPublicVisibility() { var value = new Object(); var entry = new SessionStateVariableEntry("name", value, "description"); Assert.AreEqual(ScopedItemOptions.None, entry.Options); Assert.AreEqual(SessionStateEntryVisibility.Public, entry.Visibility); Assert.AreEqual("description", entry.Description); Assert.AreEqual(value, entry.Value); Assert.AreEqual(0, entry.Attributes.Count); }
public void VariableDefinedInInitialSessionStateCanBeUsedInStatement() { string variableValue = "testVariableValue"; var variableEntry = new SessionStateVariableEntry("testVariable", variableValue, "description"); InitialSessionState sessionState = InitialSessionState.Create(); sessionState.Variables.Add(variableEntry); TestHost.InitialSessionState = sessionState; string output = TestHost.Execute("$testVariable"); Assert.AreEqual(variableValue + Environment.NewLine, output); }
public void NewVariableEntryCreatedWithSingleAttributeShouldHaveSingleAttribute() { var value = new Object(); var attribute = new CredentialAttribute(); var entry = new SessionStateVariableEntry("name", value, "description", ScopedItemOptions.Constant, attribute); Assert.AreEqual(1, entry.Attributes.Count); Assert.AreEqual(attribute, entry.Attributes[0]); Assert.AreEqual(value, entry.Value); Assert.AreEqual("name", entry.Name); Assert.AreEqual("description", entry.Description); Assert.AreEqual(ScopedItemOptions.Constant, entry.Options); }
public void NewVariableEntryCreatedWithTwoAttributesShouldHaveSingleAttributes() { var value = new Object(); var attributes = new Collection<Attribute>(); attributes.Add(new CredentialAttribute()); attributes.Add(new CredentialAttribute()); var entry = new SessionStateVariableEntry("name", value, "description", ScopedItemOptions.Constant, attributes); CollectionAssert.AreEqual(attributes, entry.Attributes); Assert.AreEqual(value, entry.Value); Assert.AreEqual("name", entry.Name); Assert.AreEqual("description", entry.Description); Assert.AreEqual(ScopedItemOptions.Constant, entry.Options); }
private static Dictionary<string, string> GetSingleAstRequiredModules(Ast ast, IEnumerable<Token> tokens, Dictionary<string,string> modules) { var 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(); var importDscResourcefunctionEntry = new SessionStateFunctionEntry( "Import-DscResource", @"param($Name, $ModuleName) if ($ModuleName) { foreach ($module in $ModuleName) { if($module.GetType().FullName -eq 'System.Collections.Hashtable'){ $mVersion = "" $mName = "" foreach($modulekey in $module.Keys){ if($modulekey -eq 'ModuleName'){ $mName = $module[$modulekey] } elseif($modulekey -eq 'ModuleVersion' -or $modulekey -eq 'RequiredVersion'){ $mVersion = $module[$modulekey] } } if(!$global:modules.ContainsKey($mName)){ $global:modules.Add($mName,$mVersion) } } else{ if(!$global:modules.ContainsKey($module)){ $global:modules.Add($module,"""") } } } } 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(); } } } var modulesFromDscResource = resources.Select(GetModuleNameForDscResource); foreach (var moduleName in modulesFromDscResource) { if (!modules.ContainsKey(moduleName)) { modules.Add(moduleName, ""); } } return modules; }
internal static Runspace CreateLocalActivityRunspace(PSLanguageMode? languageMode = null, bool useCurrentThreadForExecution = true) { InitialSessionState initialSessionStateWithSharedTypesAndNoFormat = LocalRunspaceProvider.GetInitialSessionStateWithSharedTypesAndNoFormat(); if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { initialSessionStateWithSharedTypesAndNoFormat.LanguageMode = PSLanguageMode.ConstrainedLanguage; } if (languageMode.HasValue && languageMode.HasValue) { initialSessionStateWithSharedTypesAndNoFormat.LanguageMode = languageMode.Value; } SessionStateVariableEntry sessionStateVariableEntry = new SessionStateVariableEntry("RunningInPSWorkflowEndpoint", (object)((bool)1), "True if we're in a Workflow Endpoint", ScopedItemOptions.Constant); initialSessionStateWithSharedTypesAndNoFormat.Variables.Add(sessionStateVariableEntry); Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionStateWithSharedTypesAndNoFormat); if (useCurrentThreadForExecution) { runspace.ThreadOptions = PSThreadOptions.UseCurrentThread; } return runspace; }
/// <summary> /// Creates a local runspace with the autoloading turned on. /// </summary> /// <returns></returns> internal static Runspace CreateLocalActivityRunspace(PSLanguageMode? languageMode = null, bool useCurrentThreadForExecution = true) { InitialSessionState iss = GetInitialSessionStateWithSharedTypesAndNoFormat(); if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { iss.LanguageMode = PSLanguageMode.ConstrainedLanguage; } if (languageMode != null && languageMode.HasValue) { iss.LanguageMode = languageMode.Value; } // Add a variable indicating that we're in Workflow endpoint. This enables the // autoloading feature. SessionStateVariableEntry ssve = new SessionStateVariableEntry("RunningInPSWorkflowEndpoint", true, "True if we're in a Workflow Endpoint", ScopedItemOptions.Constant); iss.Variables.Add(ssve); Runspace runspace = RunspaceFactory.CreateRunspace(iss); if (useCurrentThreadForExecution) runspace.ThreadOptions = PSThreadOptions.UseCurrentThread; return runspace; }