public void Initialise()
    {
        var packageInstaller = new PowerShellVersion();

        packageInstaller.Initialise();
        Debug.WriteLine(packageInstaller.Version);
    }
Exemple #2
0
 /// <summary>
 /// Create a new platform information collector around the given PowerShell session.
 /// </summary>
 /// <param name="pwsh">The PowerShell session to gather platform information from.</param>
 public PlatformInformationCollector(SMA.PowerShell pwsh)
 {
     _pwsh = pwsh;
     _lazyPSVersionTable           = new Lazy <Hashtable>(() => _pwsh.AddScript("$PSVersionTable").InvokeAndClear <Hashtable>().FirstOrDefault());
     _lazyPSVersion                = new Lazy <PowerShellVersion>(() => PowerShellVersion.Create(PSVersionTable["PSVersion"]));
     _lazyCurrentVersionInfo       = new Lazy <CurrentVersionInfo>(() => ReadCurrentVersionFromRegistry());
     _lazyWin32OperatingSystemInfo = new Lazy <Win32OSCimInfo>(() => GetWin32OperatingSystemInfo());
 }
 private PowerShellDataCollector(
     SMA.PowerShell pwsh,
     PowerShellVersion psVersion,
     IReadOnlyCollection <string> excludedModulePrefixes)
 {
     _pwsh      = pwsh;
     _psVersion = psVersion;
     _excludedModulePrefixes = excludedModulePrefixes;
     _lazyCommonParameters   = new Lazy <ReadOnlySet <string> >(GetPowerShellCommonParameterNames);
 }
        public async Task ServiceReturnsPowerShellVersionDetails()
        {
            PowerShellVersion versionDetails =
                await this.SendRequest(
                    PowerShellVersionRequest.Type,
                    new PowerShellVersionRequest());

            // TODO: This should be more robust and predictable.
            Assert.StartsWith("5.", versionDetails.Version);
            Assert.StartsWith("5.", versionDetails.DisplayVersion);
            Assert.Equal("Desktop", versionDetails.Edition);
            Assert.Equal("x86", versionDetails.Architecture);
        }
        public async Task CanSendPowerShellGetVersionRequest()
        {
            PowerShellVersion details
                = await LanguageClient.SendRequest <PowerShellVersion>("powerShell/getVersion", new GetVersionParams());

            if (PwshExe == "powershell")
            {
                Assert.Equal("Desktop", details.Edition);
            }
            else
            {
                Assert.Equal("Core", details.Edition);
            }
        }
Exemple #6
0
        public async Task CanSendPowerShellGetVersionRequestAsync()
        {
            PowerShellVersion details
                = await PsesLanguageClient
                  .SendRequest <GetVersionParams>("powerShell/getVersion", new GetVersionParams())
                  .Returning <PowerShellVersion>(CancellationToken.None).ConfigureAwait(false);

            if (PwshExe == "powershell")
            {
                Assert.Equal("Desktop", details.Edition);
            }
            else
            {
                Assert.Equal("Core", details.Edition);
            }
        }
 /// <summary>
 /// Build a new PowerShellDataCollector with the given configuration.
 /// </summary>
 /// <param name="pwsh">The PowerShell session to collect data from.</param>
 /// <param name="psVersion">The version of PowerShell running.</param>
 /// <returns>The constructed PowerShell data collector object.</returns>
 public PowerShellDataCollector Build(SMA.PowerShell pwsh, PowerShellVersion psVersion)
 {
     return(new PowerShellDataCollector(pwsh, psVersion, ExcludedModulePathPrefixes));
 }
        private void Initialize(HostDetails hostDetails, Runspace initialRunspace)
        {
            Validate.IsNotNull("initialRunspace", initialRunspace);

            this.SessionState = PowerShellContextState.NotStarted;

            this.initialRunspace = initialRunspace;
            this.currentRunspace = initialRunspace;

            this.currentRunspace.Debugger.BreakpointUpdated += OnBreakpointUpdated;
            this.currentRunspace.Debugger.DebuggerStop      += OnDebuggerStop;

            this.powerShell          = PowerShell.Create();
            this.powerShell.Runspace = this.currentRunspace;

            // TODO: Should this be configurable?
            this.SetExecutionPolicy(ExecutionPolicy.RemoteSigned);

            // Get the PowerShell runtime version
            this.PowerShellVersion = GetPowerShellVersion();

            // Write out the PowerShell version for tracking purposes
            Logger.Write(
                LogLevel.Normal,
                string.Format(
                    "PowerShell runtime version: {0}",
                    this.PowerShellVersion));

            if (PowerShellVersion >= new Version(5, 0))
            {
                this.versionSpecificOperations = new PowerShell5Operations();
            }
            else if (PowerShellVersion.Major == 4)
            {
                this.versionSpecificOperations = new PowerShell4Operations();
            }
            else if (PowerShellVersion.Major == 3)
            {
                this.versionSpecificOperations = new PowerShell3Operations();
            }
            else
            {
                throw new NotSupportedException(
                          "This computer has an unsupported version of PowerShell installed: " +
                          PowerShellVersion.ToString());
            }

            // Configure the runspace's debugger
            this.versionSpecificOperations.ConfigureDebugger(
                this.currentRunspace);

            // Set the $profile variable in the runspace
            this.profilePaths =
                this.SetProfileVariableInCurrentRunspace(
                    hostDetails);

            // Now that initialization is complete we can watch for InvocationStateChanged
            this.powerShell.InvocationStateChanged += powerShell_InvocationStateChanged;

            this.SessionState = PowerShellContextState.Ready;

            // Now that the runspace is ready, enqueue it for first use
            RunspaceHandle runspaceHandle = new RunspaceHandle(this.currentRunspace, this);

            this.runspaceWaitQueue.EnqueueAsync(runspaceHandle).Wait();
        }
Exemple #9
0
        // Module defining this command


        // Optional custom code for this activity


        /// <summary>
        /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run.
        /// </summary>
        /// <param name="context">The NativeActivityContext for the currently running activity.</param>
        /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns>
        /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks>
        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
        {
            System.Management.Automation.PowerShell invoker       = global::System.Management.Automation.PowerShell.Create();
            System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName);

            // Initialize the arguments

            if (Path.Expression != null)
            {
                targetCommand.AddParameter("Path", Path.Get(context));
            }

            if (NestedModules.Expression != null)
            {
                targetCommand.AddParameter("NestedModules", NestedModules.Get(context));
            }

            if (Guid.Expression != null)
            {
                targetCommand.AddParameter("Guid", Guid.Get(context));
            }

            if (Author.Expression != null)
            {
                targetCommand.AddParameter("Author", Author.Get(context));
            }

            if (CompanyName.Expression != null)
            {
                targetCommand.AddParameter("CompanyName", CompanyName.Get(context));
            }

            if (Copyright.Expression != null)
            {
                targetCommand.AddParameter("Copyright", Copyright.Get(context));
            }

            if (RootModule.Expression != null)
            {
                targetCommand.AddParameter("RootModule", RootModule.Get(context));
            }

            if (ModuleVersion.Expression != null)
            {
                targetCommand.AddParameter("ModuleVersion", ModuleVersion.Get(context));
            }

            if (Description.Expression != null)
            {
                targetCommand.AddParameter("Description", Description.Get(context));
            }

            if (ProcessorArchitecture.Expression != null)
            {
                targetCommand.AddParameter("ProcessorArchitecture", ProcessorArchitecture.Get(context));
            }

            if (PowerShellVersion.Expression != null)
            {
                targetCommand.AddParameter("PowerShellVersion", PowerShellVersion.Get(context));
            }

            if (ClrVersion.Expression != null)
            {
                targetCommand.AddParameter("ClrVersion", ClrVersion.Get(context));
            }

            if (DotNetFrameworkVersion.Expression != null)
            {
                targetCommand.AddParameter("DotNetFrameworkVersion", DotNetFrameworkVersion.Get(context));
            }

            if (PowerShellHostName.Expression != null)
            {
                targetCommand.AddParameter("PowerShellHostName", PowerShellHostName.Get(context));
            }

            if (PowerShellHostVersion.Expression != null)
            {
                targetCommand.AddParameter("PowerShellHostVersion", PowerShellHostVersion.Get(context));
            }

            if (RequiredModules.Expression != null)
            {
                targetCommand.AddParameter("RequiredModules", RequiredModules.Get(context));
            }

            if (TypesToProcess.Expression != null)
            {
                targetCommand.AddParameter("TypesToProcess", TypesToProcess.Get(context));
            }

            if (FormatsToProcess.Expression != null)
            {
                targetCommand.AddParameter("FormatsToProcess", FormatsToProcess.Get(context));
            }

            if (ScriptsToProcess.Expression != null)
            {
                targetCommand.AddParameter("ScriptsToProcess", ScriptsToProcess.Get(context));
            }

            if (RequiredAssemblies.Expression != null)
            {
                targetCommand.AddParameter("RequiredAssemblies", RequiredAssemblies.Get(context));
            }

            if (FileList.Expression != null)
            {
                targetCommand.AddParameter("FileList", FileList.Get(context));
            }

            if (ModuleList.Expression != null)
            {
                targetCommand.AddParameter("ModuleList", ModuleList.Get(context));
            }

            if (FunctionsToExport.Expression != null)
            {
                targetCommand.AddParameter("FunctionsToExport", FunctionsToExport.Get(context));
            }

            if (AliasesToExport.Expression != null)
            {
                targetCommand.AddParameter("AliasesToExport", AliasesToExport.Get(context));
            }

            if (VariablesToExport.Expression != null)
            {
                targetCommand.AddParameter("VariablesToExport", VariablesToExport.Get(context));
            }

            if (CmdletsToExport.Expression != null)
            {
                targetCommand.AddParameter("CmdletsToExport", CmdletsToExport.Get(context));
            }

            if (DscResourcesToExport.Expression != null)
            {
                targetCommand.AddParameter("DscResourcesToExport", DscResourcesToExport.Get(context));
            }

            if (PrivateData.Expression != null)
            {
                targetCommand.AddParameter("PrivateData", PrivateData.Get(context));
            }

            if (Tags.Expression != null)
            {
                targetCommand.AddParameter("Tags", Tags.Get(context));
            }

            if (ProjectUri.Expression != null)
            {
                targetCommand.AddParameter("ProjectUri", ProjectUri.Get(context));
            }

            if (LicenseUri.Expression != null)
            {
                targetCommand.AddParameter("LicenseUri", LicenseUri.Get(context));
            }

            if (IconUri.Expression != null)
            {
                targetCommand.AddParameter("IconUri", IconUri.Get(context));
            }

            if (ReleaseNotes.Expression != null)
            {
                targetCommand.AddParameter("ReleaseNotes", ReleaseNotes.Get(context));
            }

            if (HelpInfoUri.Expression != null)
            {
                targetCommand.AddParameter("HelpInfoUri", HelpInfoUri.Get(context));
            }

            if (PassThru.Expression != null)
            {
                targetCommand.AddParameter("PassThru", PassThru.Get(context));
            }

            if (DefaultCommandPrefix.Expression != null)
            {
                targetCommand.AddParameter("DefaultCommandPrefix", DefaultCommandPrefix.Get(context));
            }


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
 public void Initialise()
 {
     var packageInstaller = new PowerShellVersion();
     packageInstaller.Initialise();
     Debug.WriteLine(packageInstaller.Version);
 }