I have a separate test project to test my cmdlets project that has calls like :
pSCmdletObject.InvokeCommand.InvokeScript("Write-Host " + outputString); and
pSCmdletObject.InvokeCommand.InvokeScript("Read-Host");
I have used the PSHostUserInterface, PSRawUserInterface, PSHost implementation examples given in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Host03
Here is the problem :
TestClass.cs (of the Test project) :
using (Runspace runSpace = RunspaceFactory.CreateRunspace(new Host(new TestClass())))
{
runSpace.Open();
using (Pipeline pipeline = runSpace.CreatePipeline())
{
pipeline.Commands.Add("CustomCommand-Get");
var output = pipeline.Invoke(); //<<< (1)
}
}
At "(1)" when "Invoke" is called, the "CustomCommand" implementation written in the actual project is called. In the "CustomCommand"'s "ProcessRecord()" of the actual cmdlets project, when "Read-Host" is called, the project just waits for input which I currently can't provide because I am unable to find a way to that. My test project has the Invoke call and I want to avoid using the console. I want to somehow hard-code the inputs according to the test cases as the actual Custom Cmdlet project is an entirely different project that can run independently on its own. Can this be done? If so, how? Any other way to achieve this?
Thanks in advance
If you have a custom host implementation then you can modify that host impl to create a test host impl where you can have PSHostUserInterface.Prompt()
and PSHostUserInterface.ReadLine()
return whatever you want when these methods are called. These methods (plus PSHostUserInterface.ReadLineAsSecureString()
) are called by Read-Host
to read a string from the host (end user).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments