Пример #1
0
        /// <inheritdoc />
        protected CassiaCmdlet()
        {
            _remoteRunspace = new Lazy <Runspace>(() =>
            {
                var wsmanConnectionInfo = new WSManConnectionInfo
                {
                    ComputerName = ComputerName,
                    Credential   = Credential
                };

                wsmanConnectionInfo.SetSessionOptions(new PSSessionOption()
                {
                    IncludePortInSPN = true
                });

                var remoteRunspace = RunspaceFactory.CreateRunspace(wsmanConnectionInfo);

                remoteRunspace.Open();

                return(remoteRunspace);
            });

            var scriptRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ??
                             throw new InvalidOperationException("Failed to get assembly directory");

            var cassiaAssemblyPath = Path.Combine(scriptRoot, "Lib", "Cassia.dll");

            CassiaAssemblyBase64 = Convert.ToBase64String(File.ReadAllBytes(cassiaAssemblyPath));

            var scriptPath = Path.Combine(scriptRoot, "Scripts", "CassiaCmdlet.ps1");

            CassiaAssemblyImportScript = File.ReadAllText(scriptPath);
        }
Пример #2
0
 internal void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
 {
     connectionInfo.SetSessionOptions(this.SessionOption);
     if (!base.ParameterSetName.Equals("Uri", StringComparison.OrdinalIgnoreCase))
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (!this.allowRedirection)
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
 }
Пример #3
0
 internal void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
 {
     connectionInfo.SetSessionOptions(this.SessionOption);
     if (!base.ParameterSetName.Equals("Uri", StringComparison.OrdinalIgnoreCase))
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (!this.allowRedirection)
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
 }
Пример #4
0
 private void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
 {
     if ((base.ParameterSetName != "ConnectionUriInstanceId") && (base.ParameterSetName != "ConnectionUriSessionName"))
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (!this.allowRedirection)
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (this.SessionOption != null)
     {
         connectionInfo.SetSessionOptions(this.SessionOption);
     }
 }
Пример #5
0
 private void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
 {
     if (base.ParameterSetName != "ConnectionUri" && base.ParameterSetName != "ConnectionUriGuid")
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (!this.allowRedirection)
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (this.SessionOption != null)
     {
         connectionInfo.SetSessionOptions(this.SessionOption);
     }
 }
Пример #6
0
        /// <summary>
        /// Updates connection info with the data read from cmdlet's parameters.
        /// </summary>
        /// <param name="connectionInfo"></param>
        private void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
        {
            if (ParameterSetName != GetPSSessionCommand.ConnectionUriParameterSet &&
                ParameterSetName != GetPSSessionCommand.ConnectionUriInstanceIdParameterSet)
            {
                // uri redirection is supported only with URI parameter set
                connectionInfo.MaximumConnectionRedirectionCount = 0;
            }

            if (!_allowRedirection)
            {
                // uri redirection required explicit user consent
                connectionInfo.MaximumConnectionRedirectionCount = 0;
            }

            // Update the connectionInfo object with passed in session options.
            if (SessionOption != null)
            {
                connectionInfo.SetSessionOptions(SessionOption);
            }
        }
Пример #7
0
        } // ValidateRemoteRunspacesSpecified

        /// <summary>
        /// Updates connection info with the data read from cmdlet's parameters and
        /// sessions variables.
        /// The following data is updated:
        /// 1. MaxURIRedirectionCount
        /// 2. MaxRecvdDataSizePerSession
        /// 3. MaxRecvdDataSizePerCommand
        /// 4. MaxRecvdObjectSize
        /// </summary>
        /// <param name="connectionInfo"></param>
        internal void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
        {
            Dbg.Assert(connectionInfo != null, "connectionInfo cannot be null.");

            connectionInfo.SetSessionOptions(this.SessionOption);

            if (!ParameterSetName.Equals(PSRemotingBaseCmdlet.UriParameterSet, StringComparison.OrdinalIgnoreCase))
            {
                // uri redirection is supported only with URI parameter set
                connectionInfo.MaximumConnectionRedirectionCount = 0;
            }

            if (!_allowRedirection)
            {
                // uri redirection required explicit user consent
                connectionInfo.MaximumConnectionRedirectionCount = 0;
            }
        }
Пример #8
0
 private void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
 {
     if ((base.ParameterSetName != "ConnectionUriInstanceId") && (base.ParameterSetName != "ConnectionUriSessionName"))
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (!this.allowRedirection)
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (this.SessionOption != null)
     {
         connectionInfo.SetSessionOptions(this.SessionOption);
     }
 }
Пример #9
0
        internal static List <WSManConnectionInfo> GetConnectionInfo(string[] PSComputerName, string[] PSConnectionUri, string PSCertificateThumbprint, string PSConfigurationName, bool?PSUseSsl, int?PSPort, string PSApplicationName, PSCredential PSCredential, AuthenticationMechanism PSAuthentication, bool PSAllowRedirection, PSSessionOption options)
        {
            int defaultMaximumConnectionRedirectionCount;
            List <WSManConnectionInfo> wSManConnectionInfos = new List <WSManConnectionInfo>();

            string[] pSConnectionUri = null;
            bool     flag            = false;

            if (PSComputerName.IsNullOrEmpty() || !PSConnectionUri.IsNullOrEmpty())
            {
                if (!PSComputerName.IsNullOrEmpty() || PSConnectionUri.IsNullOrEmpty())
                {
                    throw new ArgumentException(Resources.CannotSupplyUriAndComputername);
                }
                else
                {
                    pSConnectionUri = PSConnectionUri;
                }
            }
            else
            {
                pSConnectionUri = PSComputerName;
                flag            = true;
            }
            string[] strArrays = pSConnectionUri;
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string str = strArrays[i];
                if (string.IsNullOrEmpty(str))
                {
                    wSManConnectionInfos.Add(null);
                }
                else
                {
                    WSManConnectionInfo wSManConnectionInfo = new WSManConnectionInfo();
                    if (PSPort.HasValue)
                    {
                        wSManConnectionInfo.Port = PSPort.Value;
                    }
                    if (PSUseSsl.HasValue && PSUseSsl.Value)
                    {
                        wSManConnectionInfo.Scheme = "https";
                    }
                    if (!string.IsNullOrEmpty(PSConfigurationName))
                    {
                        wSManConnectionInfo.ShellUri = PSConfigurationName;
                    }
                    if (!string.IsNullOrEmpty(PSApplicationName))
                    {
                        wSManConnectionInfo.AppName = PSApplicationName;
                    }
                    if (!flag)
                    {
                        wSManConnectionInfo.ConnectionUri = (Uri)LanguagePrimitives.ConvertTo(str, typeof(Uri), CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        wSManConnectionInfo.ComputerName = str;
                    }
                    if (PSCredential != null)
                    {
                        wSManConnectionInfo.Credential = PSCredential;
                    }
                    if (!string.IsNullOrEmpty(PSCertificateThumbprint))
                    {
                        wSManConnectionInfo.CertificateThumbprint = PSCertificateThumbprint;
                    }
                    if (PSAuthentication != AuthenticationMechanism.Default)
                    {
                        wSManConnectionInfo.AuthenticationMechanism = PSAuthentication;
                    }
                    WSManConnectionInfo wSManConnectionInfo1 = wSManConnectionInfo;
                    if (PSAllowRedirection)
                    {
                        defaultMaximumConnectionRedirectionCount = ActivityUtils.DefaultMaximumConnectionRedirectionCount;
                    }
                    else
                    {
                        defaultMaximumConnectionRedirectionCount = 0;
                    }
                    wSManConnectionInfo1.MaximumConnectionRedirectionCount = defaultMaximumConnectionRedirectionCount;
                    if (options != null)
                    {
                        wSManConnectionInfo.SetSessionOptions(options);
                    }
                    wSManConnectionInfos.Add(wSManConnectionInfo);
                }
            }
            return(wSManConnectionInfos);
        }
Пример #10
0
        internal static List<WSManConnectionInfo> GetConnectionInfo(string[] PSComputerName, string[] PSConnectionUri,
            string PSCertificateThumbprint, string PSConfigurationName,
            bool? PSUseSsl, uint? PSPort, string PSApplicationName,
            PSCredential PSCredential, AuthenticationMechanism PSAuthentication,
            bool PSAllowRedirection, System.Management.Automation.Remoting.PSSessionOption options)
        {
            List<WSManConnectionInfo> connections = new List<WSManConnectionInfo>();

            string[] machineList = null;
            bool connectByComputerName = false;

            // Connect by computername
            if ((! PSComputerName.IsNullOrEmpty()) && (PSConnectionUri.IsNullOrEmpty()))
            {
                machineList = PSComputerName;
                connectByComputerName = true;
            }
            else if ((PSComputerName.IsNullOrEmpty()) && (! PSConnectionUri.IsNullOrEmpty()))
            {
                machineList = PSConnectionUri;
            }
            else
            {
                throw new ArgumentException(Resources.CannotSupplyUriAndComputername);
            }

            // Go through each machine in the list an update its properties
            foreach (string machine in machineList)
            {
                if (!string.IsNullOrEmpty(machine))
                {
                    WSManConnectionInfo connectionInfo = new WSManConnectionInfo();

                    if (PSPort.HasValue)
                    {
                        connectionInfo.Port = (int)PSPort.Value;
                    }

                    if (PSUseSsl.HasValue && (PSUseSsl.Value))
                    {
                        connectionInfo.Scheme = WSManConnectionInfo.HttpsScheme;
                    }

                    if (!String.IsNullOrEmpty(PSConfigurationName))
                    {
                        connectionInfo.ShellUri = PSConfigurationName;
                    }

                    if (!String.IsNullOrEmpty(PSApplicationName))
                    {
                        connectionInfo.AppName = PSApplicationName;
                    }

                    if (connectByComputerName)
                    {
                        connectionInfo.ComputerName = machine;
                    }
                    else
                    {
                        connectionInfo.ConnectionUri = (Uri)LanguagePrimitives.ConvertTo(machine, typeof(Uri), System.Globalization.CultureInfo.InvariantCulture);
                    }

                    if (PSCredential != null)
                    {
                        connectionInfo.Credential = PSCredential;
                    }

                    if (!String.IsNullOrEmpty(PSCertificateThumbprint))
                    {
                        connectionInfo.CertificateThumbprint = PSCertificateThumbprint;
                    }

                    if (PSAuthentication != AuthenticationMechanism.Default)
                    {
                        connectionInfo.AuthenticationMechanism = PSAuthentication;
                    }

                    connectionInfo.MaximumConnectionRedirectionCount = PSAllowRedirection ? DefaultMaximumConnectionRedirectionCount : 0;

                    if (options != null)
                    {
                        connectionInfo.SetSessionOptions(options);
                    }

                    connections.Add(connectionInfo);
                }
                else
                {
                    // add a null connection to account for "" or $null in PSComputerName parameter
                    connections.Add(null);
                }
            }

            return connections;
        }
Пример #11
0
        /// <summary>
        /// Updates connection info with the data read from cmdlet's parameters.
        /// </summary>
        /// <param name="connectionInfo"></param>
        private void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
        {
            if (ParameterSetName != ReceivePSSessionCommand.ConnectionUriInstanceIdParameterSet &&
                ParameterSetName != ReceivePSSessionCommand.ConnectionUriSessionNameParameterSet)
            {
                // uri redirection is supported only with URI parameter set
                connectionInfo.MaximumConnectionRedirectionCount = 0;
            }

            if (!_allowRedirection)
            {
                // uri redirection required explicit user consent
                connectionInfo.MaximumConnectionRedirectionCount = 0;
            }

            // Update the connectionInfo object with passed in session options.
            if (SessionOption != null)
            {
                connectionInfo.SetSessionOptions(SessionOption);
            }
        }
        public override bool Validate()
        {
            // TODO failures in create and cleanup should be ignored for couple of times
            // TODO Add retries in validation logic
            // TODO talk to guest agent and gets it version and validate if latest
            object t = Impersonation.Impersonate(ConfigurationManager.UserName, ConfigurationManager.Password);

            try
            {
                var token      = AzureHelper.GetAccessTokenAsync();
                var credential = new TokenCredentials(token.Result.AccessToken);

                PublicIPAddress ipAddress = AzureHelper.GetPublicAddressAsync(credential, groupName, subscriptionId, "myPublicIP").Result;

                // TODO: make username-password at one place instead of at both ARM and here
                SecureString securePwd = new SecureString();
                password.ToCharArray().ToList().ForEach(p => securePwd.AppendChar(p));

                // this is the entrypoint to interact with the system (interfaced for testing).
                var remoteComputer = new Uri(string.Format("{0}://{1}:5986", "https", ipAddress.IpAddress));

                var connection = new WSManConnectionInfo(remoteComputer, String.Empty, new PSCredential(username, securePwd));

                var option = new PSSessionOption();
                option.SkipCACheck         = true;
                option.SkipCNCheck         = true;
                option.SkipRevocationCheck = true;
                connection.SetSessionOptions(option);

                connection.AuthenticationMechanism = AuthenticationMechanism.Negotiate;

                // TODO What if powershell session gets stuck in between

                var runspace = RunspaceFactory.CreateRunspace(connection);
                runspace.Open();

                var powershell = PowerShell.Create();
                powershell.Runspace = runspace;

                powershell.AddScript("get-psdrive –psprovider filesystem");
                var results = powershell.Invoke();
                foreach (var output in results.Where(o => o != null))
                {
                }

                bool ifCustomData = true;
                if (!string.IsNullOrWhiteSpace(customData))
                {
                    powershell.AddScript("Get-Content C:\\AzureData\\CustomData.bin -Encoding UTF8");
                    results      = powershell.Invoke();
                    ifCustomData = (results.Where(o => o != null).ToList().Count == 1);
                    foreach (var output in results.Where(o => o != null))
                    {
                        logger.Info(string.Format("Expected: {0} Actual: {1}", customData, output));
                        ifCustomData &= (output.ToString() == customData);
                    }
                }

                runspace.Close();

                if (!ifCustomData)
                {
                    throw new ArgumentException("Incorrect custom data!!");
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(false);
            }
            finally
            {
                Impersonation.UndoImpersonation(t);
            }

            return(true);
        }
Пример #13
0
        internal static List <WSManConnectionInfo> GetConnectionInfo(string[] PSComputerName, string[] PSConnectionUri,
                                                                     string PSCertificateThumbprint, string PSConfigurationName,
                                                                     bool?PSUseSsl, uint?PSPort, string PSApplicationName,
                                                                     PSCredential PSCredential, AuthenticationMechanism PSAuthentication,
                                                                     bool PSAllowRedirection, System.Management.Automation.Remoting.PSSessionOption options)
        {
            List <WSManConnectionInfo> connections = new List <WSManConnectionInfo>();

            string[] machineList           = null;
            bool     connectByComputerName = false;

            // Connect by computername
            if ((!PSComputerName.IsNullOrEmpty()) && (PSConnectionUri.IsNullOrEmpty()))
            {
                machineList           = PSComputerName;
                connectByComputerName = true;
            }
            else if ((PSComputerName.IsNullOrEmpty()) && (!PSConnectionUri.IsNullOrEmpty()))
            {
                machineList = PSConnectionUri;
            }
            else
            {
                throw new ArgumentException(Resources.CannotSupplyUriAndComputername);
            }

            // Go through each machine in the list an update its properties
            foreach (string machine in machineList)
            {
                if (!string.IsNullOrEmpty(machine))
                {
                    WSManConnectionInfo connectionInfo = new WSManConnectionInfo();

                    if (PSPort.HasValue)
                    {
                        connectionInfo.Port = (int)PSPort.Value;
                    }

                    if (PSUseSsl.HasValue && (PSUseSsl.Value))
                    {
                        connectionInfo.Scheme = WSManConnectionInfo.HttpsScheme;
                    }

                    if (!String.IsNullOrEmpty(PSConfigurationName))
                    {
                        connectionInfo.ShellUri = PSConfigurationName;
                    }

                    if (!String.IsNullOrEmpty(PSApplicationName))
                    {
                        connectionInfo.AppName = PSApplicationName;
                    }

                    if (connectByComputerName)
                    {
                        connectionInfo.ComputerName = machine;
                    }
                    else
                    {
                        connectionInfo.ConnectionUri = (Uri)LanguagePrimitives.ConvertTo(machine, typeof(Uri), System.Globalization.CultureInfo.InvariantCulture);
                    }

                    if (PSCredential != null)
                    {
                        connectionInfo.Credential = PSCredential;
                    }

                    if (!String.IsNullOrEmpty(PSCertificateThumbprint))
                    {
                        connectionInfo.CertificateThumbprint = PSCertificateThumbprint;
                    }

                    if (PSAuthentication != AuthenticationMechanism.Default)
                    {
                        connectionInfo.AuthenticationMechanism = PSAuthentication;
                    }

                    connectionInfo.MaximumConnectionRedirectionCount = PSAllowRedirection ? DefaultMaximumConnectionRedirectionCount : 0;

                    if (options != null)
                    {
                        connectionInfo.SetSessionOptions(options);
                    }

                    connections.Add(connectionInfo);
                }
                else
                {
                    // add a null connection to account for "" or $null in PSComputerName parameter
                    connections.Add(null);
                }
            }

            return(connections);
        }
Пример #14
0
 private void UpdateConnectionInfo(WSManConnectionInfo connectionInfo)
 {
     if (base.ParameterSetName != "ConnectionUri" && base.ParameterSetName != "ConnectionUriGuid")
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (!this.allowRedirection)
     {
         connectionInfo.MaximumConnectionRedirectionCount = 0;
     }
     if (this.SessionOption != null)
     {
         connectionInfo.SetSessionOptions(this.SessionOption);
     }
 }
Пример #15
0
		internal static List<WSManConnectionInfo> GetConnectionInfo(string[] PSComputerName, string[] PSConnectionUri, string PSCertificateThumbprint, string PSConfigurationName, bool? PSUseSsl, int? PSPort, string PSApplicationName, PSCredential PSCredential, AuthenticationMechanism PSAuthentication, bool PSAllowRedirection, PSSessionOption options)
		{
			int defaultMaximumConnectionRedirectionCount;
			List<WSManConnectionInfo> wSManConnectionInfos = new List<WSManConnectionInfo>();
			string[] pSConnectionUri = null;
			bool flag = false;
			if (PSComputerName.IsNullOrEmpty() || !PSConnectionUri.IsNullOrEmpty())
			{
				if (!PSComputerName.IsNullOrEmpty() || PSConnectionUri.IsNullOrEmpty())
				{
					throw new ArgumentException(Resources.CannotSupplyUriAndComputername);
				}
				else
				{
					pSConnectionUri = PSConnectionUri;
				}
			}
			else
			{
				pSConnectionUri = PSComputerName;
				flag = true;
			}
			string[] strArrays = pSConnectionUri;
			for (int i = 0; i < (int)strArrays.Length; i++)
			{
				string str = strArrays[i];
				if (string.IsNullOrEmpty(str))
				{
					wSManConnectionInfos.Add(null);
				}
				else
				{
					WSManConnectionInfo wSManConnectionInfo = new WSManConnectionInfo();
					if (PSPort.HasValue)
					{
						wSManConnectionInfo.Port = PSPort.Value;
					}
					if (PSUseSsl.HasValue && PSUseSsl.Value)
					{
						wSManConnectionInfo.Scheme = "https";
					}
					if (!string.IsNullOrEmpty(PSConfigurationName))
					{
						wSManConnectionInfo.ShellUri = PSConfigurationName;
					}
					if (!string.IsNullOrEmpty(PSApplicationName))
					{
						wSManConnectionInfo.AppName = PSApplicationName;
					}
					if (!flag)
					{
						wSManConnectionInfo.ConnectionUri = (Uri)LanguagePrimitives.ConvertTo(str, typeof(Uri), CultureInfo.InvariantCulture);
					}
					else
					{
						wSManConnectionInfo.ComputerName = str;
					}
					if (PSCredential != null)
					{
						wSManConnectionInfo.Credential = PSCredential;
					}
					if (!string.IsNullOrEmpty(PSCertificateThumbprint))
					{
						wSManConnectionInfo.CertificateThumbprint = PSCertificateThumbprint;
					}
					if (PSAuthentication != AuthenticationMechanism.Default)
					{
						wSManConnectionInfo.AuthenticationMechanism = PSAuthentication;
					}
					WSManConnectionInfo wSManConnectionInfo1 = wSManConnectionInfo;
					if (PSAllowRedirection)
					{
						defaultMaximumConnectionRedirectionCount = ActivityUtils.DefaultMaximumConnectionRedirectionCount;
					}
					else
					{
						defaultMaximumConnectionRedirectionCount = 0;
					}
					wSManConnectionInfo1.MaximumConnectionRedirectionCount = defaultMaximumConnectionRedirectionCount;
					if (options != null)
					{
						wSManConnectionInfo.SetSessionOptions(options);
					}
					wSManConnectionInfos.Add(wSManConnectionInfo);
				}
			}
			return wSManConnectionInfos;
		}