Пример #1
0
        private static Dictionary <string, object> GetUsingValuesAsDictionary(IEnumerable <UsingExpressionAst> usingAsts, PSCmdlet psCmdlet)
        {
            Dictionary <string, object> usingValues = new Dictionary <string, object>();

            foreach (var usingAst in usingAsts)
            {
                var varAst = usingAst.SubExpression as VariableExpressionAst;
                if (varAst == null)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture,
                                            Properties.Resources.ResourceManager.GetString("UsingNotVariableExpression"),
                                            new object[] { usingAst.Extent.Text });
                    throw new PSInvalidOperationException(msg);
                }

                try
                {
                    var usingValue = psCmdlet.GetVariableValue(varAst.VariablePath.UserPath);
                    var usingKey   = GetUsingExpressionKey(usingAst);
                    if (!usingValues.ContainsKey(usingKey))
                    {
                        usingValues.Add(usingKey, usingValue);
                    }
                }
                catch (Exception ex)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture,
                                            Properties.Resources.ResourceManager.GetString("UsingVariableNotFound"),
                                            new object[] { usingAst.Extent.Text });
                    throw new PSInvalidOperationException(msg, ex);
                }
            }

            return(usingValues);
        }
Пример #2
0
 public static ActionPreference ErrorAction(this PSCmdlet cmdlet)
 {
     if (cmdlet.MyInvocation.BoundParameters.ContainsKey("ErrorAction"))
     {
         return((ActionPreference)cmdlet.MyInvocation.BoundParameters["ErrorAction"]);
     }
     return(cmdlet.GetVariableValue <ActionPreference>("ErrorActionPreference"));
 }
Пример #3
0
        private object ExecuteScriptAndGetVariable(PSCmdlet cmdlet, string scriptFormatString)
        {
            string        outputVariable = "outputVariable";
            List <object> cmdletResults  = cmdlet.ExecuteScript <object>(string.Format(scriptFormatString, outputVariable));
            var           output         = cmdlet.GetVariableValue(outputVariable);

            return(output);
        }
Пример #4
0
        private string ExecuteScriptAndGetVariable(PSCmdlet cmdlet, string scriptFormatString, string defaultValue)
        {
            string outputVariable = "outputVariable";

            cmdlet.ExecuteScript <object>(string.Format(scriptFormatString, outputVariable));
            var output = cmdlet.GetVariableValue(outputVariable, defaultValue);

            return(output.ToString());
        }
Пример #5
0
        public void RunWebAppContainerPSSessionScript(PSCmdlet cmdlet, string resourceGroupName, string webSiteName, string slotName = null, bool newPSSession = false)
        {
            Version psCurrentVersion = GetPsVersion(cmdlet);
            Version psCore           = new Version(6, 0);

            if (psCurrentVersion.CompareTo(psCore) < 0)
            {
                // Running on PowerShell 5.1. Assume Windows.

                // Validate if WSMAN Basic Authentication is enabled
                bool isBasicAuthEnabled = ExecuteScriptAndGetVariableAsBool(cmdlet, "${0} = (Get-Item WSMAN:\\LocalHost\\Client\\Auth\\Basic -ErrorAction SilentlyContinue).Value", false);

                if (!isBasicAuthEnabled)
                {
                    // Ask user to enable basic auth
                    WriteWarning(Properties.Resources.EnterCotnainerPSSessionBasicAuthWarning);
                    return;
                }

                // Validate if we can connect given the existing TrustedHosts
                string defaultTrustedHostsScriptResult = "<empty or non-existent>";
                string trustedHostsScriptResult        = ExecuteScriptAndGetVariable(cmdlet, "${0} = (Get-Item WSMAN:\\LocalHost\\Client\\TrustedHosts -ErrorAction SilentlyContinue).Value", defaultTrustedHostsScriptResult);

                Regex expression = new Regex(@"^\*$|((^\*|^" + (string.IsNullOrWhiteSpace(slotName)? webSiteName : webSiteName + "-" + slotName) + ").azurewebsites.net)");

                if (trustedHostsScriptResult.Split(',').Where(h => expression.IsMatch(h)).Count() < 1)
                {
                    WriteWarning(string.Format(Properties.Resources.EnterContainerPSSessionFormatForTrustedHostsWarning, string.IsNullOrWhiteSpace(trustedHostsScriptResult) ? defaultTrustedHostsScriptResult: trustedHostsScriptResult) +
                                 Environment.NewLine +
                                 Environment.NewLine +
                                 string.Format(@Properties.Resources.EnterContainerPSSessionFormatForTrustedHostsSuggestion,
                                               string.IsNullOrWhiteSpace(trustedHostsScriptResult) ? string.Empty : trustedHostsScriptResult + ",",
                                               (string.IsNullOrWhiteSpace(slotName) ? webSiteName : webSiteName + "-" + slotName)));

                    return;
                }
            }

            Site         site = GetWebApp(resourceGroupName, webSiteName, slotName);
            User         user = GetPublishingCredentials(resourceGroupName, webSiteName, slotName);
            const string webAppContainerPSSessionVarPrefix = "webAppPSSession";
            string       publishingUserName = user.PublishingUserName.Length <= 20 ? user.PublishingUserName : user.PublishingUserName.Substring(0, 20);

            string psSessionScript = string.Format("${3}User = '******' \n${3}Password = ConvertTo-SecureString -String '{1}' -AsPlainText -Force \n" +
                                                   "${3}Credential = New-Object -TypeName PSCredential -ArgumentList ${3}User, ${3}Password\n" +
                                                   (newPSSession ? "${3}NewPsSession = New-PSSession" : "Enter-PSSession") + " -ConnectionUri https://{2}/WSMAN -Authentication Basic -Credential ${3}Credential \n",
                                                   publishingUserName, user.PublishingPassword, site.DefaultHostName, webAppContainerPSSessionVarPrefix);

            cmdlet.ExecuteScript <object>(psSessionScript);
            if (newPSSession)
            {
                cmdlet.WriteObject(cmdlet.GetVariableValue(string.Format("{0}NewPsSession", webAppContainerPSSessionVarPrefix)));
            }
            cmdlet.ExecuteScript <object>(string.Format("Clear-Variable {0}*", webAppContainerPSSessionVarPrefix)); //Clearing session variable
        }
        public DomainDispatcher(PSCmdlet cmdlet)
        {
            // Not using Check here because this assembly is very small and without resources
            if (cmdlet == null)
            {
                throw new ArgumentNullException("cmdlet");
            }

            _cmdlet = cmdlet;
            _dte    = (DTE)cmdlet.GetVariableValue("DTE");
        }
Пример #7
0
        internal static PSEdition GetPSEdition(PSCmdlet cmdlet)
        {
            var variable = cmdlet.GetVariableValue("global:PSEdition")?.ToString();

            switch (variable)
            {
            case "Desktop":
                return(PSEdition.Desktop);

            case "Core":
                return(PSEdition.Core);

            default:
                return(PSEdition.Other);
            }
        }
Пример #8
0
        public void RunWebAppContainerPSSessionScript(PSCmdlet cmdlet, string resourceGroupName, string webSiteName, string slotName = null, bool newPSSession = false)
        {
            string operatingSystem = GetPsOperatingSystem(cmdlet);

            Version minimumVersion = new Version(6, 1, 0, 0);

            WriteVerbose("Operating System: {0}", operatingSystem);

            if (operatingSystem.IndexOf("windows", StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                // If OS is not Windows, check if Ps supports 6.1.0 which is the first version to depend on NetCoreApp 2.1

                List <Version> compatibleVersions = GetPsCompatibleVersions(cmdlet);

                foreach (Version version in compatibleVersions)
                {
                    WriteVerbose("Compatible version: {0}", version.ToString());
                }

                // if there are no compatible versions subsequent to the minimum versions, we don't continue because the command will fail
                if (compatibleVersions.Where(v => v.CompareTo(minimumVersion) > 0).Count() == 0)
                {
                    WriteError(Properties.Resources.EnterContainerPSSessionPSCoreVersionNotSupported);

                    return;
                }
            }

            // For Windows, we validate WSMAN trusted hosts settings
            if (operatingSystem.IndexOf("windows", StringComparison.InvariantCultureIgnoreCase) > 0)
            {
                // Validate if WSMAN Basic Authentication is enabled
                bool isBasicAuthEnabled = ExecuteScriptAndGetVariableAsBool(cmdlet, "${0} = (Get-Item WSMAN:\\LocalHost\\Client\\Auth\\Basic -ErrorAction SilentlyContinue).Value", false);

                if (!isBasicAuthEnabled)
                {
                    // Ask user to enable basic auth
                    WriteWarning(Properties.Resources.EnterCotnainerPSSessionBasicAuthWarning);
                    return;
                }

                // Validate if we can connect given the existing TrustedHosts
                string defaultTrustedHostsScriptResult = "<empty or non-existent>";
                string trustedHostsScriptResult        = ExecuteScriptAndGetVariable(cmdlet, "${0} = (Get-Item WSMAN:\\LocalHost\\Client\\TrustedHosts -ErrorAction SilentlyContinue).Value", defaultTrustedHostsScriptResult);

                Regex expression = new Regex(@"^\*$|((^\*|^" + (string.IsNullOrWhiteSpace(slotName)? webSiteName : webSiteName + "-" + slotName) + ").azurewebsites.net)");

                if (trustedHostsScriptResult.Split(',').Where(h => expression.IsMatch(h)).Count() < 1)
                {
                    WriteWarning(string.Format(Properties.Resources.EnterContainerPSSessionFormatForTrustedHostsWarning, string.IsNullOrWhiteSpace(trustedHostsScriptResult) ? defaultTrustedHostsScriptResult: trustedHostsScriptResult) +
                                 Environment.NewLine +
                                 Environment.NewLine +
                                 string.Format(@Properties.Resources.EnterContainerPSSessionFormatForTrustedHostsSuggestion,
                                               string.IsNullOrWhiteSpace(trustedHostsScriptResult) ? string.Empty : trustedHostsScriptResult + ",",
                                               (string.IsNullOrWhiteSpace(slotName) ? webSiteName : webSiteName + "-" + slotName)));

                    return;
                }
            }



            Site         site = GetWebApp(resourceGroupName, webSiteName, slotName);
            User         user = GetPublishingCredentials(resourceGroupName, webSiteName, slotName);
            const string webAppContainerPSSessionVarPrefix = "webAppPSSession";
            string       publishingUserName = user.PublishingUserName.Length <= 20 ? user.PublishingUserName : user.PublishingUserName.Substring(0, 20);

            string psSessionScript = string.Format("${3}User = '******' \n${3}Password = ConvertTo-SecureString -String '{1}' -AsPlainText -Force \n" +
                                                   "${3}Credential = New-Object -TypeName PSCredential -ArgumentList ${3}User, ${3}Password\n" +
                                                   (newPSSession ? "${3}NewPsSession = New-PSSession" : "Enter-PSSession") + " -ConnectionUri https://{2}/WSMAN -Authentication Basic -Credential ${3}Credential \n",
                                                   publishingUserName, user.PublishingPassword, site.DefaultHostName, webAppContainerPSSessionVarPrefix);

            cmdlet.ExecuteScript <object>(psSessionScript);
            if (newPSSession)
            {
                cmdlet.WriteObject(cmdlet.GetVariableValue(string.Format("{0}NewPsSession", webAppContainerPSSessionVarPrefix)));
            }
            cmdlet.ExecuteScript <object>(string.Format("Clear-Variable {0}*", webAppContainerPSSessionVarPrefix)); //Clearing session variable
        }
Пример #9
0
 private static bool IsISE(PSCmdlet cmdlet) => cmdlet.GetVariableValue("global:psISE") != null;
 public static T GetPsVariable <T>(this PSCmdlet cmdlet, string name, object defaultValue = null)
 {
     return((T)cmdlet.GetVariableValue(name, defaultValue));
 }