示例#1
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
        }
示例#2
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);
        }
示例#3
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());
        }
示例#4
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
        }