Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing 2 apps one with c# and the other with powershell 1.0, in some point of my code I want to pass a string that indicating the server name from my c# app to a powershell script file that I wrote, how do I send it? and how do i accept it?

my code :

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();

String scriptfile = @"c:\test.ps1";

Command myCommand = new Command(scriptfile, false);
CommandParameter testParam = new CommandParameter("username", "serverName");

myCommand.Parameters.Add(testParam);


pipeline.Commands.Add(myCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
runspace.Close();

and my powershell script

param([string]$Username)

write-host $username 

What am I missing? I'm kinda new with powershell.

share|improve this question
Try to remove everything from your powershell file except the "write-host $username" line – laszlokiss88 Apr 11 at 12:39
What happens when you run that? Does the powershell script not run at all, or give the wrong output, or give no output at all, or ...? If you run that powershell script directly from powershell does it work? – Nate Hekman Apr 11 at 14:44
i found an answer it was this: Go to Start Menu and search for "Windows PowerShell ISE". Right click the x86 version and choose "Run as administrator". In the top part, paste Set-ExecutionPolicy RemoteSigned; run the script. Choose "Yes". but now i got a new problem. now i get: A positional parameter cannot be found that accepts argument '$null'. any ideas? – woolford Apr 11 at 14:53

1 Answer

I have machines with PowerShell 2.0 and 3.0 but not 1.0, so my results may differ. When I run your code on my PowerShell 3.0 box, I get:

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

It didn't like the Write-Host, so I changed your script to

param([string]$Username)

Get-Date
Get-ChildItem -Path $userName

Get-Date so that I could see some output without depending on the parameter and GCI to use the parameter. I modified your code to look like this:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
    runspace.Open();

    String scriptfile = @"..\..\..\test.ps1";
    String path = @"C:\Users\Public\";

    var pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add(new Command("Set-ExecutionPolicy RemoteSigned -Scope Process", true));
    pipeline.Invoke();

    pipeline = runspace.CreatePipeline();
    var myCommand = new Command(scriptfile, false);
    var testParam = new CommandParameter("username", path);
    myCommand.Parameters.Add(testParam);
    pipeline.Commands.Add(myCommand);
    var psObjects = pipeline.Invoke();
    foreach (var obj in psObjects)
    {
        Console.WriteLine(obj.ToString());
    }
    runspace.Close();
}

Console.WriteLine("Press a key to continue...");
Console.ReadKey(true);

and it ran without error and displayed the folder contents, on both PoSh 2 and 3.

For info, if you're only setting the execution policy for the current process, you don't need to run elevated, hence I was able to do it in-code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.