bin/projects/dbatools/dbatools/Parameter/DbaCmConnectionParameter.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Sqlcollaborative.Dbatools.Connection;

namespace Sqlcollaborative.Dbatools.Parameter
{
        
    /// <summary>
    /// Input converter for Computer Management Information
    /// </summary>
    public class DbaCmConnectionParameter
    {
        #region Fields of contract
        /// <summary>
        /// The resolved connection object
        /// </summary>
        [ParameterContract(ParameterContractType.Field, ParameterContractBehavior.Mandatory | ParameterContractBehavior.Conditional)]
        public ManagementConnection Connection;

        /// <summary>
        /// Whether input processing was successful
        /// </summary>
        [ParameterContract(ParameterContractType.Field, ParameterContractBehavior.Mandatory | ParameterContractBehavior.Arbiter)]
        public bool Success;

        /// <summary>
        /// The object actually passed to the class
        /// </summary>
        [ParameterContract(ParameterContractType.Field, ParameterContractBehavior.Mandatory)]
        public object InputObject;
        #endregion Fields of contract

        /// <summary>
        /// Implicitly convert all connection parameter objects to the connection-type
        /// </summary>
        /// <param name="Input">The parameter object to convert</param>
        [ParameterContract(ParameterContractType.Operator, ParameterContractBehavior.Conversion)]
        public static implicit operator ManagementConnection(DbaCmConnectionParameter Input)
        {
            return Input.Connection;
        }

        /// <summary>
        /// Creates a new DbaWmConnectionParameter based on an input-name
        /// </summary>
        /// <param name="ComputerName">The name of the computer the connection is stored for.</param>
        public DbaCmConnectionParameter(string ComputerName)
        {
            InputObject = ComputerName;
            if (!Utility.Validation.IsValidComputerTarget(ComputerName))
            {
                Success = false;
                return;
            }


            bool test = false;
            try { test = ConnectionHost.Connections[ComputerName.ToLower()] != null; }
            catch { }

            if (test)
            {
                Connection = ConnectionHost.Connections[ComputerName.ToLower()];
            }

            else
            {
                Connection = new ManagementConnection(ComputerName.ToLower());
                ConnectionHost.Connections[Connection.ComputerName] = Connection;
            }

            Success = true;
        }

        /// <summary>
        /// Creates a new DbaWmConnectionParameter based on an already existing connection object.
        /// </summary>
        /// <param name="Connection">The connection to accept</param>
        public DbaCmConnectionParameter(ManagementConnection Connection)
        {
            InputObject = Connection;

            this.Connection = Connection;

            Success = true;
        }

        /// <summary>
        /// Tries to convert a generic input object into a true input.
        /// </summary>
        /// <param name="Input">Any damn object in the world</param>
        public DbaCmConnectionParameter(object Input)
        {
            InputObject = Input;
            PSObject tempInput = new PSObject(Input);
            string typeName = "";

            try { typeName = tempInput.TypeNames[0].ToLower(); }
            catch
            {
                Success = false;
                return;
            }

            switch (typeName)
            {
                case "Sqlcollaborative.Dbatools.connection.managementconnection":
                    try
                    {
                        ManagementConnection con = new ManagementConnection();
                        con.ComputerName = (string)tempInput.Properties["ComputerName"].Value;

                        con.CimRM = (ManagementConnectionProtocolState)tempInput.Properties["CimRM"].Value;
                        con.LastCimRM = (DateTime)tempInput.Properties["LastCimRM"].Value;
                        con.CimDCOM = (ManagementConnectionProtocolState)tempInput.Properties["CimDCOM"].Value;
                        con.LastCimDCOM = (DateTime)tempInput.Properties["LastCimDCOM"].Value;
                        con.Wmi = (ManagementConnectionProtocolState)tempInput.Properties["Wmi"].Value;
                        con.LastWmi = (DateTime)tempInput.Properties["LastWmi"].Value;
                        con.PowerShellRemoting = (ManagementConnectionProtocolState)tempInput.Properties["PowerShellRemoting"].Value;
                        con.LastPowerShellRemoting = (DateTime)tempInput.Properties["LastPowerShellRemoting"].Value;

                        con.Credentials = (PSCredential)tempInput.Properties["Credentials"].Value;
                        con.OverrideExplicitCredential = (bool)tempInput.Properties["OverrideExplicitCredential"].Value;
                        con.KnownBadCredentials = (List<PSCredential>)tempInput.Properties["KnownBadCredentials"].Value;
                        con.WindowsCredentialsAreBad = (bool)tempInput.Properties["WindowsCredentialsAreBad"].Value;
                        con.UseWindowsCredentials = (bool)tempInput.Properties["UseWindowsCredentials"].Value;

                        con.DisableBadCredentialCache = (bool)tempInput.Properties["DisableBadCredentialCache"].Value;
                        con.DisableCimPersistence = (bool)tempInput.Properties["DisableCimPersistence"].Value;
                        con.DisableCredentialAutoRegister = (bool)tempInput.Properties["DisableCredentialAutoRegister"].Value;
                        con.EnableCredentialFailover = (bool)tempInput.Properties["EnableCredentialFailover"].Value;

                    }
                    catch
                    {
                        Success = false;
                    }
                    break;

                default:
                    Success = false;
                    break;
            }
        }

        /// <summary>
        /// Creates a new DbaCmConnectionParameter based on an instance parameter
        /// </summary>
        /// <param name="Instance">The instance to interpret</param>
        public DbaCmConnectionParameter(DbaInstanceParameter Instance)
            : this(Instance.ComputerName)
        {

        }
    }
}