コード例 #1
0
        /// <summary>
        /// Creates a Command object from a PSObject property bag.
        /// PSObject has to be in the format returned by ToPSObjectForRemoting method.
        /// </summary>
        /// <param name="commandAsPSObject">PSObject to rehydrate.</param>
        /// <returns>
        /// Command rehydrated from a PSObject property bag
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if the PSObject is null.
        /// </exception>
        /// <exception cref="System.Management.Automation.Remoting.PSRemotingDataStructureException">
        /// Thrown when the PSObject is not in the expected format
        /// </exception>
        internal static Command FromPSObjectForRemoting(PSObject commandAsPSObject)
        {
            if (commandAsPSObject == null)
            {
                throw PSTraceSource.NewArgumentNullException("commandAsPSObject");
            }

            string  commandText           = RemotingDecoder.GetPropertyValue <string>(commandAsPSObject, RemoteDataNameStrings.CommandText);
            bool    isScript              = RemotingDecoder.GetPropertyValue <bool>(commandAsPSObject, RemoteDataNameStrings.IsScript);
            bool?   useLocalScopeNullable = RemotingDecoder.GetPropertyValue <bool?>(commandAsPSObject, RemoteDataNameStrings.UseLocalScopeNullable);
            Command command = new Command(commandText, isScript, useLocalScopeNullable);

            // For V2 backwards compatibility.
            PipelineResultTypes mergeMyResult = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeMyResult);
            PipelineResultTypes mergeToResult = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeToResult);

            command.MergeMyResults(mergeMyResult, mergeToResult);

            command.MergeUnclaimedPreviousCommandResults = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeUnclaimedPreviousCommandResults);

            // V3 merge instructions will not be returned by V2 server and this is expected.
            if (commandAsPSObject.Properties[RemoteDataNameStrings.MergeError] != null)
            {
                command.MergeInstructions[(int)MergeType.Error] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeError);
            }

            if (commandAsPSObject.Properties[RemoteDataNameStrings.MergeWarning] != null)
            {
                command.MergeInstructions[(int)MergeType.Warning] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeWarning);
            }

            if (commandAsPSObject.Properties[RemoteDataNameStrings.MergeVerbose] != null)
            {
                command.MergeInstructions[(int)MergeType.Verbose] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeVerbose);
            }

            if (commandAsPSObject.Properties[RemoteDataNameStrings.MergeDebug] != null)
            {
                command.MergeInstructions[(int)MergeType.Debug] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeDebug);
            }

            if (commandAsPSObject.Properties[RemoteDataNameStrings.MergeInformation] != null)
            {
                command.MergeInstructions[(int)MergeType.Information] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, RemoteDataNameStrings.MergeInformation);
            }

            foreach (PSObject parameterAsPSObject in RemotingDecoder.EnumerateListProperty <PSObject>(commandAsPSObject, RemoteDataNameStrings.Parameters))
            {
                command.Parameters.Add(CommandParameter.FromPSObjectForRemoting(parameterAsPSObject));
            }

            return(command);
        }
コード例 #2
0
        internal static Command FromPSObjectForRemoting(PSObject commandAsPSObject)
        {
            if (commandAsPSObject == null)
            {
                throw PSTraceSource.NewArgumentNullException("commandAsPSObject");
            }
            string              propertyValue = RemotingDecoder.GetPropertyValue <string>(commandAsPSObject, "Cmd");
            bool                isScript      = RemotingDecoder.GetPropertyValue <bool>(commandAsPSObject, "IsScript");
            bool?               useLocalScope = RemotingDecoder.GetPropertyValue <bool?>(commandAsPSObject, "UseLocalScope");
            Command             command       = new Command(propertyValue, isScript, useLocalScope);
            PipelineResultTypes myResult      = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeMyResult");
            PipelineResultTypes toResult      = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeToResult");

            command.MergeMyResults(myResult, toResult);
            command.MergeUnclaimedPreviousCommandResults = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergePreviousResults");
            if (commandAsPSObject.Properties["MergeError"] != null)
            {
                command.MergeInstructions[0] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeError");
            }
            if (commandAsPSObject.Properties["MergeWarning"] != null)
            {
                command.MergeInstructions[1] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeWarning");
            }
            if (commandAsPSObject.Properties["MergeVerbose"] != null)
            {
                command.MergeInstructions[2] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeVerbose");
            }
            if (commandAsPSObject.Properties["MergeDebug"] != null)
            {
                command.MergeInstructions[3] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeDebug");
            }
            foreach (PSObject obj2 in RemotingDecoder.EnumerateListProperty <PSObject>(commandAsPSObject, "Args"))
            {
                command.Parameters.Add(CommandParameter.FromPSObjectForRemoting(obj2));
            }
            return(command);
        }