Beispiel #1
0
        public static RemoteRunspace FromPSObjectForRemoting(PSObject obj)
        {
            int    id   = RemotingDecoder.GetPropertyValue <int> (obj, "Id");
            string name = RemotingDecoder.GetPropertyValue <string> (obj, "Name");
            PSPrimitiveDictionary appArgs         = RemotingDecoder.GetPropertyValue <PSPrimitiveDictionary> (obj, "ApplicationArguments");
            string   connectionInfoType           = RemotingDecoder.GetPropertyValue <string> (obj, "ConnectionInfoType");
            PSObject connectionObj                = RemotingDecoder.GetPropertyValue <PSObject> (obj, "ConnectionInfo");
            RunspaceConnectionInfo connectionInfo = null;

            if (connectionInfoType == "WSManConnectionInfo")
            {
                connectionInfo = WSManConnectionInfo.FromPSObjectForRemoting(connectionObj);
            }
            else
            {
                var securePassord = new System.Security.SecureString();
                securePassord.AppendChar('z');
                connectionInfo = new NewProcessConnectionInfo(new PSCredential("Anonymous", securePassord));
            }
            var runspace = new RemoteRunspace(new TypeTable(), connectionInfo, null, appArgs, name, id);

            runspace._version                          = RemotingDecoder.GetPropertyValue <Version>(obj, "Version");
            runspace.ApartmentState                    = RemotingDecoder.GetPropertyValue <ApartmentState>(obj, "ApartmentState");
            runspace.InstanceId                        = RemotingDecoder.GetPropertyValue <Guid>(obj, "InstanceId");
            runspace._shouldCloseOnPop                 = RemotingDecoder.GetPropertyValue <bool>(obj, "ShouldCloseOnPop");
            runspace._runspaceStateInfo                = new System.Management.Automation.Runspaces.RunspaceStateInfo(RemotingDecoder.GetPropertyValue <RunspaceState>(obj, "RunspaceStateInfo"));
            runspace._runspaceAvailability             = RemotingDecoder.GetPropertyValue <RunspaceAvailability>(obj, "RunspaceAvailability");
            runspace.ThreadOptions                     = RemotingDecoder.GetPropertyValue <PSThreadOptions>(obj, "ThreadOptions");
            runspace.EngineActivityId                  = RemotingDecoder.GetPropertyValue <Guid>(obj, "EngineActivityId");
            runspace._bSessionStateProxyCallInProgress = RemotingDecoder.GetPropertyValue <bool>(obj, "SessionStateProxyCallInProgress");
            runspace._bypassRunspaceStateCheck         = RemotingDecoder.GetPropertyValue <bool>(obj, "ByPassRunspaceStateCheck");
            PSEtwLog.SetActivityIdForCurrentThread(runspace.InstanceId);
            string   originalConnectionInfoType           = RemotingDecoder.GetPropertyValue <string> (obj, "OriginalConnectionInfoType");
            PSObject originalConnectionObj                = RemotingDecoder.GetPropertyValue <PSObject> (obj, "OriginalConnectionInfo");
            RunspaceConnectionInfo originalConnectionInfo = null;

            if (originalConnectionInfoType == "WSManConnectionInfo")
            {
                originalConnectionInfo = WSManConnectionInfo.FromPSObjectForRemoting(originalConnectionObj);
            }
            else
            {
                var securePassord = new System.Security.SecureString();
                securePassord.AppendChar('z');
                originalConnectionInfo = new NewProcessConnectionInfo(new PSCredential("Anonymous", securePassord));
            }
            runspace._originalConnectionInfo = originalConnectionInfo;
            runspace.RunspacePool.RemoteRunspacePoolInternal.SetStateInfo(new RunspacePoolStateInfo(RunspacePoolState.Opened, null));
            return(runspace);
        }
        internal static RemoteSessionCapability GetSessionCapability(object data)
        {
            RemoteSessionCapability sessionCapability = data is PSObject psObject ? new RemoteSessionCapability(RemotingDestination.InvalidDestination, RemotingDecoder.GetPropertyValue <Version>(psObject, "protocolversion"), RemotingDecoder.GetPropertyValue <Version>(psObject, "PSVersion"), RemotingDecoder.GetPropertyValue <Version>(psObject, "SerializationVersion")) : throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastRemotingDataToPSObject, new object[1]
            {
                (object)data.GetType().FullName
            });

            if (psObject.Properties["TimeZone"] != null)
            {
                byte[] propertyValue = RemotingDecoder.GetPropertyValue <byte[]>(psObject, "TimeZone");
                sessionCapability.TimeZone = RemoteSessionCapability.ConvertFromByteToTimeZone(propertyValue);
            }
            return(sessionCapability);
        }
Beispiel #3
0
        internal static ProgressRecord FromPSObjectForRemoting(PSObject progressAsPSObject)
        {
            if (progressAsPSObject == null)
            {
                throw PSTraceSource.NewArgumentNullException("progressAsPSObject");
            }
            string propertyValue = RemotingDecoder.GetPropertyValue <string>(progressAsPSObject, "Activity");
            int    activityId    = RemotingDecoder.GetPropertyValue <int>(progressAsPSObject, "ActivityId");

            return(new ProgressRecord(activityId, propertyValue, RemotingDecoder.GetPropertyValue <string>(progressAsPSObject, "StatusDescription"))
            {
                CurrentOperation = RemotingDecoder.GetPropertyValue <string>(progressAsPSObject, "CurrentOperation"), ParentActivityId = RemotingDecoder.GetPropertyValue <int>(progressAsPSObject, "ParentActivityId"), PercentComplete = RemotingDecoder.GetPropertyValue <int>(progressAsPSObject, "PercentComplete"), RecordType = RemotingDecoder.GetPropertyValue <ProgressRecordType>(progressAsPSObject, "Type"), SecondsRemaining = RemotingDecoder.GetPropertyValue <int>(progressAsPSObject, "SecondsRemaining")
            });
        }
 internal static ApartmentState GetApartmentState(object data)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(RemotingDecoder.GetPropertyValue <ApartmentState>(PSObject.AsPSObject(data), "ApartmentState"));
 }
 internal static RemoteStreamOptions GetRemoteStreamOptions(object data)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(RemotingDecoder.GetPropertyValue <RemoteStreamOptions>(PSObject.AsPSObject(data), "RemoteStreamOptions"));
 }
 internal static PSInvocationStateInfo GetPowerShellStateInfo(object data)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(data is PSObject psObject ? new PSInvocationStateInfo(RemotingDecoder.GetPropertyValue <PSInvocationState>(psObject, "PipelineState"), RemotingDecoder.GetExceptionFromStateInfoObject(psObject)) : throw new PSRemotingDataStructureException(PSRemotingErrorId.DecodingErrorForPowerShellStateInfo, new object[0]));
 }
        internal static PowerShell GetCommandDiscoveryPipeline(object data)
        {
            using (RemotingDecoder._trace.TraceMethod())
            {
                PSObject     psObject     = PSObject.AsPSObject(data);
                CommandTypes commandTypes = psObject != null?RemotingDecoder.GetPropertyValue <CommandTypes>(psObject, "CommandType") : throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastRemotingDataToPSObject, new object[1]
                {
                    (object)data.GetType().FullName
                });

                string[] strArray1;
                if (RemotingDecoder.GetPropertyValue <PSObject>(psObject, "Name") != null)
                {
                    strArray1 = new List <string>(RemotingDecoder.EnumerateListProperty <string>(psObject, "Name")).ToArray();
                }
                else
                {
                    strArray1 = new string[1] {
                        "*"
                    }
                };
                string[] strArray2;
                if (RemotingDecoder.GetPropertyValue <PSObject>(psObject, "Namespace") != null)
                {
                    strArray2 = new List <string>(RemotingDecoder.EnumerateListProperty <string>(psObject, "Namespace")).ToArray();
                }
                else
                {
                    strArray2 = new string[1] {
                        ""
                    }
                };
                object[]   objArray   = RemotingDecoder.GetPropertyValue <PSObject>(psObject, "ArgumentList") == null ? (object[])null : new List <object>(RemotingDecoder.EnumerateListProperty <object>(psObject, "ArgumentList")).ToArray();
                PowerShell powerShell = PowerShell.Create();
                powerShell.AddCommand("Get-Command");
                powerShell.AddParameter("Name", (object)strArray1);
                powerShell.AddParameter("CommandType", (object)commandTypes);
                powerShell.AddParameter("Module", (object)strArray2);
                powerShell.AddParameter("ArgumentList", (object)objArray);
                return(powerShell);
            }
        }
 internal static PSThreadOptions GetThreadOptions(PSObject dataAsPSObject)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(dataAsPSObject != null?RemotingDecoder.GetPropertyValue <PSThreadOptions>(dataAsPSObject, "PSThreadOptions") : throw RemotingDecoder._trace.NewArgumentNullException(nameof(dataAsPSObject)));
 }
 internal static HostInfo GetHostInfo(PSObject dataAsPSObject)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(dataAsPSObject != null?RemoteHostEncoder.DecodeObject((object)RemotingDecoder.GetPropertyValue <PSObject>(dataAsPSObject, "HostInfo"), typeof(HostInfo)) as HostInfo : throw RemotingDecoder._trace.NewArgumentNullException(nameof(dataAsPSObject)));
 }
Beispiel #10
0
 internal static PSPrimitiveDictionary GetApplicationArguments(
     PSObject dataAsPSObject)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(dataAsPSObject != null?RemotingDecoder.GetPropertyValue <PSPrimitiveDictionary>(dataAsPSObject, "ApplicationArguments") : throw RemotingDecoder._trace.NewArgumentNullException(nameof(dataAsPSObject)));
 }
Beispiel #11
0
 internal static int GetMaxRunspaces(PSObject dataAsPSObject)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(dataAsPSObject != null?RemotingDecoder.GetPropertyValue <int>(dataAsPSObject, "MaxRunspaces") : throw RemotingDecoder._trace.NewArgumentNullException(nameof(dataAsPSObject)));
 }
Beispiel #12
0
 internal static string GetEncryptedSessionKey(PSObject dataAsPSObject) => dataAsPSObject != null?RemotingDecoder.GetPropertyValue <string>(dataAsPSObject, "EncryptedSessionKey") : throw RemotingDecoder._trace.NewArgumentNullException(nameof(dataAsPSObject));
Beispiel #13
0
 internal static RunspacePoolStateInfo GetRunspacePoolStateInfo(
     PSObject dataAsPSObject)
 {
     using (RemotingDecoder._trace.TraceMethod())
         return(dataAsPSObject != null ? new RunspacePoolStateInfo(RemotingDecoder.GetPropertyValue <RunspacePoolState>(dataAsPSObject, "RunspaceState"), RemotingDecoder.GetExceptionFromStateInfoObject(dataAsPSObject)) : throw RemotingDecoder._trace.NewArgumentNullException(nameof(dataAsPSObject)));
 }
Beispiel #14
0
        private static T ConvertPropertyValueTo <T>(string propertyName, object propertyValue)
        {
            using (RemotingDecoder._trace.TraceMethod())
            {
                if (propertyName == null)
                {
                    throw RemotingDecoder._trace.NewArgumentNullException(nameof(propertyName));
                }
                if (typeof(T).IsEnum)
                {
                    if (propertyValue is string)
                    {
                        try
                        {
                            return((T)Enum.Parse(typeof(T), (string)propertyValue, true));
                        }
                        catch (ArgumentException ex)
                        {
                            throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3]
                            {
                                (object)propertyName,
                                (object)typeof(T).FullName,
                                (object)propertyValue.GetType().FullName
                            });
                        }
                    }
                    else
                    {
                        try
                        {
                            Type underlyingType = Enum.GetUnderlyingType(typeof(T));
                            return((T)LanguagePrimitives.ConvertTo(propertyValue, underlyingType, (IFormatProvider)CultureInfo.InvariantCulture));
                        }
                        catch (InvalidCastException ex)
                        {
                            throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3]
                            {
                                (object)propertyName,
                                (object)typeof(T).FullName,
                                (object)propertyValue.GetType().FullName
                            });
                        }
                    }
                }
                else
                {
                    if (typeof(T).Equals(typeof(PSObject)))
                    {
                        return(propertyValue == null ? default(T) : (T)PSObject.AsPSObject(propertyValue));
                    }
                    switch (propertyValue)
                    {
                    case null:
                        if (!typeof(T).IsValueType)
                        {
                            return(default(T));
                        }
                        if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                        {
                            return(default(T));
                        }
                        throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3]
                        {
                            (object)propertyName,
                            (object)typeof(T).FullName,
                            propertyValue != null ? (object)propertyValue.GetType().FullName : (object)"null"
                        });

                    case T obj:
                        return(obj);

                    case PSObject _:
                        PSObject psObject = (PSObject)propertyValue;
                        return(RemotingDecoder.ConvertPropertyValueTo <T>(propertyName, psObject.BaseObject));

                    case Hashtable _:
                        if (typeof(T).Equals(typeof(PSPrimitiveDictionary)))
                        {
                            try
                            {
                                return((T) new PSPrimitiveDictionary((Hashtable)propertyValue));
                            }
                            catch (ArgumentException ex)
                            {
                                throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3]
                                {
                                    (object)propertyName,
                                    (object)typeof(T).FullName,
                                    propertyValue != null ? (object)propertyValue.GetType().FullName : (object)"null"
                                });
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3]
                    {
                        (object)propertyName,
                        (object)typeof(T).FullName,
                        (object)propertyValue.GetType().FullName
                    });
                }
            }
        }