Example #1
0
        public AbstractCmdletTest()
        {
            _pwrSh = System.Management.Automation.PowerShell.Create();
            // Import Module
            _pwrSh.AddCommand("import-module");
            _pwrSh.AddParameter("Name", "./Sentral.Api.PowerShell.dll");
            _pwrSh.Invoke();
            _pwrSh.Commands.Clear();


            var settings = TestSettings.LoadSettings();

            try
            {
                ConnectSentralApi.GetSentralAPIConnection();
            }
            catch
            {
                var connection = new ConnectSentralApi()
                {
                    BaseUrl    = settings.BaseUrl,
                    ApiKey     = settings.ApiKey,
                    TenantCode = settings.ApiTenant
                };

                var enumerator = connection.Invoke().GetEnumerator();

                while (enumerator.MoveNext())
                {
                }
            }


            _isTestSite = settings.BaseUrl.Contains(".sentral.school/");
        }
Example #2
0
 internal RemoteHelpInfo(
     ExecutionContext context,
     RemoteRunspace remoteRunspace,
     string remoteHelpTopic,
     string remoteHelpCategory,
     HelpCategory localHelpCategory)
     : base(localHelpCategory)
 {
     using (System.Management.Automation.PowerShell powerShell = System.Management.Automation.PowerShell.Create())
     {
         powerShell.AddCommand("Get-Help");
         powerShell.AddParameter("Name", (object)remoteHelpTopic);
         if (!string.IsNullOrEmpty(remoteHelpCategory))
         {
             powerShell.AddParameter("Category", (object)remoteHelpCategory);
         }
         powerShell.Runspace = (Runspace)remoteRunspace;
         Collection <PSObject> collection;
         using (new PowerShellStopper(context, powerShell))
         {
             using (RemoteHelpInfo.tracer.TraceScope("Downloading remote help for {0}", (object)remoteHelpTopic))
                 collection = powerShell.Invoke();
         }
         this.deserializedRemoteHelp = collection != null && collection.Count != 0 ? collection[0] : throw new HelpNotFoundException(remoteHelpTopic);
         this.deserializedRemoteHelp.TypeNames.Clear();
         this.deserializedRemoteHelp.TypeNames.Add("MamlCommandHelpInfo");
         this.deserializedRemoteHelp.TypeNames.Add("HelpInfo");
         this.deserializedRemoteHelp.Methods.Remove("ToString");
     }
 }
Example #3
0
        /// <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 System.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 (GetIsComputerNameSpecified(context))
            {
                targetCommand.AddParameter("ComputerName", PSComputerName.Get(context));
            }

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

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

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

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

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

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

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


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
        // Additional 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 System.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 (Namespace.Expression != null)
            {
                targetCommand.AddParameter("Namespace", Namespace.Get(context));
            }

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

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

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

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

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

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

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

            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
Example #5
0
        private void SetupAppRootPathAndModulePath(FunctionLoadRequest functionLoadRequest, string managedDependenciesPath)
        {
            FunctionLoader.SetupWellKnownPaths(functionLoadRequest, managedDependenciesPath);

            if (FunctionLoader.FunctionAppRootPath == null)
            {
                throw new InvalidOperationException(PowerShellWorkerStrings.FunctionAppRootNotResolved);
            }

            _firstPwshInstance.AddCommand("Microsoft.PowerShell.Management\\Set-Content")
            .AddParameter("Path", "env:PSModulePath")
            .AddParameter("Value", FunctionLoader.FunctionModulePath)
            .InvokeAndClearCommands();
        }
Example #6
0
        protected Collection <T> RunScript <T>(string cmdletName, Dictionary <string, object> scriptParameters)
        {
            // specify the script code to run.
            _pwrSh.AddCommand(cmdletName);
            // specify the parameters to pass into the script.
            _pwrSh.AddParameters(scriptParameters);
            // execute the script and await the result.
            Collection <T> results = _pwrSh.Invoke <T>();

            // Reset for next
            _pwrSh.Commands.Clear();

            return(results);
        }
Example #7
0
        /// <summary>
        /// A sample application that uses the PowerShell runtime along with a host
        /// implementation to call get-process and display the results as you
        /// would see them in pwrsh.exe.
        /// </summary>
        /// <param name="args">Parameter not used.</param>
        private static void Main(string[] args)
        {
            // Set the current culture to German. We want this to be picked up when the MyHost
            // instance is created...
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-de");

            // Create the runspace, but this time we aren't using the RunspaceInvoke
            // class
            MyHost myHost = new MyHost(new Host02());

            using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost))
            {
                myRunSpace.Open();

                // Create a PowerShell to execute our commands...
                using (PowerShell powershell = PowerShell.Create())
                {
                    powershell.Runspace = myRunSpace;

                    // Add the script we want to run. The script does two things. It runs get-process with
                    // the output sorted by handle count, get-date piped to out-string so we can see the
                    // date being displayed in German...
                    powershell.AddScript(@"
                        get-process | sort handlecount
                        # This should display the date in German...
                        get-date | out-string
                        ");

                    // Now add the default outputter to the end of the pipe and indicate
                    // that it should handle both output and errors from the previous
                    // commands. This will result in the output being written using the PSHost
                    // and PSHostUserInterface classes instead of returning objects to the hosting
                    // application.
                    powershell.AddCommand("out-default");

                    powershell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

                    // Now just invoke the application - there won't be any objects returned -
                    // they're all consumed by out-default so we don't have to do anything more...
                    powershell.Invoke();
                }
            }

            System.Console.WriteLine("Hit any key to exit...");
            System.Console.ReadKey();
        }
Example #8
0
        /// <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
            // Specified ClassName cannot be WhiteSpace or NULL
            //
            if (ClassName.Expression != null && !string.IsNullOrWhiteSpace(ClassName.Get(context)))
            {
                targetCommand.AddParameter("ClassName", ClassName.Get(context));
            }

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

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

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

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

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


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
        /// <summary>
        /// Process markdown as path.
        /// </summary>
        /// <param name="parameter">Name of parameter to pass to `ConvertFrom-Markdown`.</param>
        /// <param name="input">Value of parameter.</param>
        private void ConvertFromMarkdown(string parameter, object input)
        {
            _powerShell.AddCommand("Microsoft.PowerShell.Utility\\ConvertFrom-Markdown").AddParameter(parameter, input);
            if (!UseBrowser)
            {
                _powerShell.AddParameter("AsVT100EncodedString");
            }

            Collection <MarkdownInfo> output = _powerShell.Invoke <MarkdownInfo>();

            if (_powerShell.HadErrors)
            {
                foreach (ErrorRecord errorRecord in _powerShell.Streams.Error)
                {
                    WriteError(errorRecord);
                }
            }

            foreach (MarkdownInfo result in output)
            {
                ProcessMarkdownInfo(result);
            }
        }
Example #10
0
        // Module defining this command


        // Additional 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 System.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 (UICulture.Expression != null)
            {
                targetCommand.AddParameter("UICulture", UICulture.Get(context));
            }

            if (BaseDirectory.Expression != null)
            {
                targetCommand.AddParameter("BaseDirectory", BaseDirectory.Get(context));
            }
            //If BaseDirectory is not specified, try to use the workflow base directory.
            else
            {
                throw new ArgumentException(GeneratedActivitiesResources.ImportLocalizedDataWithEmptyEmptyorNullBaseDirectory);
            }

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

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

            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
        /// <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 (NoEncryption.Expression != null)
            {
                targetCommand.AddParameter("NoEncryption", NoEncryption.Get(context));
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
        /// <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 System.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 (ClassName.Expression != null)
            {
                targetCommand.AddParameter("ClassName", ClassName.Get(context));
            }

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

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

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

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

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

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

            if (ClientOnly.Expression != null)
            {
                // Retrieve our host overrides
                var      hostValues   = context.GetExtension <HostParameterDefaults>();
                string[] computerName = null;

                if (hostValues != null)
                {
                    Dictionary <string, object> incomingArguments = hostValues.Parameters;
                    if (incomingArguments.ContainsKey("PSComputerName"))
                    {
                        computerName = incomingArguments["PSComputerName"] as string[];
                    }
                }

                if (computerName == null)
                {
                    targetCommand.AddParameter("ClientOnly", ClientOnly.Get(context));
                }
            }


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
    static void Main(string[] args)
    {
        System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create();
        List <Microsoft.InternationalSettings.Commands.WinUserLanguage> userLangList = ps.AddCommand("Get-WinUserLanguageList").Invoke()[0].BaseObject as List <Microsoft.InternationalSettings.Commands.WinUserLanguage>;

        foreach (Microsoft.InternationalSettings.Commands.WinUserLanguage userLang in userLangList)
        {
            Console.WriteLine("{0,-31}{1,-47}", "Antonym", userLang.Autonym);
            Console.WriteLine("{0,-31}{1,-47}", "EnglishName", userLang.EnglishName);
            Console.WriteLine("{0,-31}{1,-47}", "Handwriting", userLang.Handwriting);
            Console.WriteLine("{0,-31}{1,-47}", "InputMethodTips", String.Join(",", userLang.InputMethodTips));
            Console.WriteLine("{0,-31}{1,-47}", "LanguageTag", userLang.LanguageTag);
            Console.WriteLine("{0,-31}{1,-47}", "LocalizedName", userLang.LocalizedName);
            Console.WriteLine("{0,-31}{1,-47}", "ScriptName", userLang.ScriptName);
            Console.WriteLine("{0,-31}{1,-47}", "Spellchecking", userLang.Spellchecking);
            Console.WriteLine();
        }
    }