/// <summary> /// Gets all the out/ref parameter values. /// </summary> /// <param name="proxy">SessionStateProxy instance</param> /// <param name="paramsLength">Length of parameters</param> internal void GetAllOutParameterValues(SessionStateProxy proxy, int paramsLength) { if (paramsLength > 0) { int outParamIndex = 0; if (outParameterNames.Count > 0) { outArguments = new object[outParameterNames.Count]; foreach (string outParam in outParameterNames) { object outArgument = proxy.GetVariable(outParam); if (outArgument != null) { if (outArgument.GetType().IsValueType) { outArguments[outParamIndex++] = outArgument; } else { outArguments[outParamIndex++] = ResolveObjectFromPSObject(outArgument); } } else { outArguments[outParamIndex++] = null; } } } } }
/// <summary> /// Sets all parameters/properties as variables. /// </summary> /// <param name="proxy">SessionStateProxy instance.</param> /// <param name="args">The argument to be passed to the script.</param> /// <param name="helpMessage">he help message from the attribute.</param> internal void SetAllParametersAsVariables(SessionStateProxy proxy, object[] args, string helpMessage) { //set help message as variable proxy.SetVariable("PtfHelpMessage", helpMessage); //set all parameters as variables foreach (ParameterInfo pi in methodInfo.GetParameters()) { string parameterName = pi.Name; object argumentValue = null; if (pi.ParameterType.IsByRef) { if (pi.IsOut) { //sets all "out" parameters by default value argumentValue = pi.DefaultValue; } else { argumentValue = args[pi.Position]; } //stores all out/ref parameters outParameterNames.Add(parameterName); } else { argumentValue = args[pi.Position]; } proxy.SetVariable(parameterName, argumentValue); } }
private void SetPTFVariables( SessionStateProxy proxy) { //set all properties as variables foreach (string key in this.TestSite.Properties.AllKeys) { string propName = "PTFProp_" + key.Replace(".", "_"); proxy.SetVariable(propName, this.TestSite.Properties[key]); } }
/// <summary> /// Provides access to session variables. /// </summary> /// <param name="sessionState">The session state to access</param> /// <returns>An object providing access to session variables</returns> public static dynamic Variables(this SessionStateProxy sessionState) { return(new SessionVariables(sessionState.PSVariable)); }
/// <summary> /// Invokde script by given file path and arguments. /// </summary> /// <param name="path">The file path to the cmd script.</param> /// <param name="targetMethod">The method the caller invoked.</param> /// <param name="args">The argument to be passed to the script.</param> /// <param name="helpMessage">The help message from the attribute.</param> /// <returns>The return value of script executation.</returns> private PSParameterBuilder InvokeScript(string path, MethodInfo targetMethod, object[] args, string helpMessage) { //the patameter builder to handle all parameters PSParameterBuilder builder = null; //use the Dot operation in PowerShell to make all variables can be accessed in whole runspace. string scriptContent = string.Format(". \"{0}\"", Path.GetFullPath(path)); //call static method, and create the instance of runspace type Runspace runspace = RunspaceFactory.CreateRunspace(); //open run space runspace.Open(); //call runspace.CreatePipeline to create an instance of Pipeline Pipeline pipeline = runspace.CreatePipeline(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { //set execution policy for Windows in order to load and run local script files pipeline.Commands.AddScript("Set-ExecutionPolicy -Scope Process RemoteSigned"); } pipeline.Commands.AddScript(scriptContent); SessionStateProxy sessionStateProxy = runspace.SessionStateProxy; //set current location to the folder containing the script sessionStateProxy.Path.SetLocation(Path.GetDirectoryName(path)); //set variables which can be used in PowerShell script SetPTFVariables(sessionStateProxy); //set all parameters as variables which can be used //by users directly in the PowerShell script builder = new PSParameterBuilder(targetMethod); builder.SetAllParametersAsVariables(sessionStateProxy, args, helpMessage); try { if (builder != null) { //invoke script and get the return value and out/ref parameters if (builder.HasRetValue) { Collection <PSObject> returnValueCollection = pipeline.Invoke(); //get return value object KeyValuePair <string, object> retValue = GetRetValueFromCollection(returnValueCollection); if (retValue.Value != null) { if (builder.RetType.IsInstanceOfType(retValue.Value)) { builder.RetValue = retValue.Value; } else { throw new InvalidOperationException("The returned type is mismatched"); } } else { builder.RetValue = null; } } else { pipeline.Invoke(); } //get out parameters values builder.GetAllOutParameterValues( sessionStateProxy, targetMethod.GetParameters().Length); } else { pipeline.Invoke(); } //check errors in the error pipeline CheckErrorsInPipeline(pipeline); } catch (RuntimeException ex) { string errorMessage = ex.Message; string traceInfo = ex.ErrorRecord.InvocationInfo.PositionMessage; string ptfAdFailureMessage = string.Format( "Exception thrown in PowerShell Adapter: {0} {1}", errorMessage, traceInfo); throw new InvalidOperationException(ptfAdFailureMessage); } //close runspace and release resources runspace.Close(); return(builder); }