Example #1
0
 public void CheckSemantic(ClassNode node, IScope scope = null)
 {
     if (TypeTable.IsDefinedType(node.TypeClass, out _))
     {
         Logger.LogError(node.Line, node.CharPositionInLine,
                         $"Duplicate definition '{node.TypeClass}'");
         return;
     }
     TypeTable.AddType(new CoolType(node.TypeClass));
 }
Example #2
0
        /// <summary>
        /// Create the Runspace pool.
        /// For remote runspaces, load the datatypes optionally to support serialization
        /// For local commands, load the modules and snapins required or requested
        /// </summary>
        /// <param name="commandInfo"></param>
        /// <param name="pSHost"></param>
        /// <param name="maxRunspaces"></param>
        /// <param name="debugStrings"></param>
        /// <param name="useRemotePS"></param>
        /// <param name="loadAllTypedata"></param>
        /// <param name="modules"></param>
        /// <param name="modulesPath"></param>
        /// <param name="snapIns"></param>
        /// <param name="variableEntries"></param>
        /// <returns></returns>
        internal static RunspacePool CreateRunspacePool(CommandInfo commandInfo,
                                                        PSHost pSHost,
                                                        int maxRunspaces,
                                                        out List <string> debugStrings,
                                                        PSSession useRemotePS,
                                                        bool loadAllTypedata,
                                                        string[] modules = null, string[] modulesPath = null, string[] snapIns = null, IList <SessionStateVariableEntry> variableEntries = null)
        {
            debugStrings = new List <string>();
            RunspaceConnectionInfo runspaceConnectionInfo = null;
            Hashtable modulePrivatedata = commandInfo.Module?.PrivateData as Hashtable;

            ModuleDetails moduleDetails = GetModuleDetails(commandInfo, debugStrings);

            //special handling for remote PSsession commands
            if (moduleDetails.IsFromRemotingModule || useRemotePS != null)
            {
                if (useRemotePS != null)
                {
                    debugStrings.Add("Using the supplied remote PSSession");
                    runspaceConnectionInfo = useRemotePS.Runspace.ConnectionInfo;
                }
                else
                {
                    debugStrings.Add("Using remote PSSession to execute the command");
                    PSObject remotepSModule = ScriptBlock.Create(
                        string.Format("Get-Module {0}", commandInfo.ModuleName)).InvokeReturnAsIs() as PSObject;
                    if (remotepSModule.BaseObject is PSModuleInfo remotepSModuleInfo)
                    {
                        PSObject remotePs = ScriptBlock.Create(
                            string.Format("Get-PSSession | Where-Object{{$_.state -eq 'opened' -and (\"{0}\").Contains($_.ComputerName)}} | Select-Object -First 1",
                                          remotepSModuleInfo.Description)).InvokeReturnAsIs() as PSObject;

                        if (remotePs.BaseObject is PSSession remotePSSession)
                        {
                            runspaceConnectionInfo = remotePSSession.Runspace.ConnectionInfo;
                            if (modules != null || modulesPath != null)
                            {
                                debugStrings.Add("Modules were specified to load, but they will not be loaded as the command supplied is from a remote PSSession");
                            }
                        }
                        else
                        {
                            debugStrings.Add(string.Format("Command - Get-PSSession | Where-Object{{$_.state -eq 'opened' -and (\"{0}\").Contains($_.ComputerName)}} " +
                                                           "| Select-Object -First 1 - was ran to find the PSSession", remotepSModuleInfo.Description));
                            throw new Exception("Unable to find a PSSession to use. You may try passing the PSSession to use using Parameter -UseRemotePSSession");
                        }
                    }
                }

                debugStrings.Add(string.Format("Using connection info {0}", runspaceConnectionInfo.ComputerName));
                TypeTable typeTable = TypeTable.LoadDefaultTypeFiles();

                if (loadAllTypedata)
                {
                    Collection <PSObject> typeDatas = ScriptBlock.Create("Get-TypeData").Invoke();
                    foreach (PSObject typeData in typeDatas)
                    {
                        TypeData t = (TypeData)typeData.BaseObject;
                        try
                        {
                            typeTable.AddType(t);
                            debugStrings.Add(string.Format("Added typedata{0}", t.TypeName));
                        }
                        catch (Exception e)
                        {
                            debugStrings.Add(string.Format("Unable to add typeData {0}. Error {1}", t.TypeName, e.Message));
                        }
                    }
                }
                return(RunspaceFactory.CreateRunspacePool(1, Environment.ProcessorCount, runspaceConnectionInfo, pSHost, typeTable));
            }

            InitialSessionState iss               = InitialSessionState.CreateDefault2();
            List <string>       modulesToLoad     = new List <String>();
            List <string>       snapInsToLoad     = new List <string>();
            PSSnapInException   pSSnapInException = new PSSnapInException();

            if (modules?.Count() > 0)
            {
                modulesToLoad.AddRange(modules);
            }
            if (snapIns?.Count() > 0)
            {
                snapInsToLoad.AddRange(snapIns);
            }

            //Populate ISS with the snapins and modules from the moduleDetails
            LoadISSWithModuleDetails(moduleDetails, iss);

            //Load user specified snapins and modules
            if (modules?.Count() > 0 && modules.Contains("All", StringComparer.OrdinalIgnoreCase))
            {
                var modulesAvailable = ScriptBlock.Create("Get-Module -ListAvailable | Select-Object -ExpandProperty Name").Invoke();
                modulesToLoad.AddRange(from module in modulesAvailable select module.BaseObject as string);
                debugStrings.Add(string.Format("Loaded all the available modules on this computer, {0} modules found", modulesToLoad.Count));
            }

            if (modules?.Count() > 0 && modules.Contains("Loaded", StringComparer.OrdinalIgnoreCase))
            {
                var modulesLoaded = ScriptBlock.Create("Get-Module | Select-Object -ExpandProperty Name").Invoke();
                modulesToLoad.AddRange(from module in modulesLoaded select module.BaseObject as string);
                debugStrings.Add(string.Format("Loaded the modules loaded on current Runspace, {0} modules found", modulesLoaded.Count));
            }
            debugStrings.Add("Loading Modules:");
            debugStrings.AddRange(modulesToLoad);
            iss.ImportPSModule(modulesToLoad.ToArray());

            snapInsToLoad.ForEach(s => iss.ImportPSSnapIn(s, out pSSnapInException));

            if (variableEntries != null)
            {
                iss.Variables.Add(variableEntries);
            }

            return(RunspaceFactory.CreateRunspacePool(1, maxRunspaces, iss, pSHost));
        }