0

My PS script returns a string.

Function GetData {
Param(
    [string]$id
)
Process
{
    return "Value is $id"
}

GetData -id $arg

The below is the C# that calls the PS script:

        PowerShell ps = PowerShell.Create();
        string psScript = "GetData.ps1";
        ps.AddScript(psScript);
        // only takes one parameter
        ps.AddParameter("25");
        Collection<PSObject> results = ps.Invoke();

        foreach (PSObject r in results)
        {
            Console.WriteLine(r.ToString());
        }
        Console.ReadLine();

Nothing returns.

I double checked the script and it does return a value when I pass in the path manually when calling the script directly in PowerShell. I also made sure that in the Properties of the project the Platform target is x64 (based on another question's error). I also tried to directly save the result in the Invoke method, but it gave an error, which showed that I have to actually save it in a collection, even though it's one record.

Forgot, also tried:

psParam = "25";
string psScript = "GetData.ps1 -arg'" + psParam + "'";

And no result on the console.

Tested this:

        PowerShell ps = PowerShell.Create();
        string psScript = ".\\GetData.ps1";
        ps.AddCommand(psScript);
        ps.AddArgument("25");
        Collection<PSObject> results = ps.Invoke();

        foreach (PSObject r in results)
        {
            Console.WriteLine(r.ToString());
        }
        Console.ReadLine();

And used most of the above and this errors because it says GetData.ps1 is not recognized as the name of a cmdlet, function, script file, or operable program. If I point directly to it by placing it on my C drive (C:\GetData.ps1), it does nothing.

Double check; inside the script I am calling the function on the last line:

GetData -id $arg

Is this correct?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
rob
  • 95
  • 1
  • 1
  • 6
  • Show content of `GetData.ps1` and `ps.Streams.Error` after calling `ps.Invoke()`. You likely need to use `ps.AddCommand(".\\GetData.ps1")` instead of `ps.AddScript("GetData.ps1")`. – user4003407 Jun 29 '15 at 01:42
  • @PetSerAl Updated with it and did try using `AddCommand` (replacing `AddScript`) and `AddArgument` (replacing `AddParameter`) methods. – rob Jun 29 '15 at 01:51
  • 1
    Try to specify full path to `GetData.ps1`. – user4003407 Jun 29 '15 at 01:57
  • @PetSerAl If I create the function on `C:\GetData.ps1`, it will attempt it, but return nothing (original problem). If I call it in the project folder, it errors with *GetData.ps1 is not recognized as the name of a cmdlet, function, script file, or operable program*. – rob Jun 29 '15 at 02:04
  • Are you sure that your `GetData.ps1` return data, not write it to host or anything? Try to make it as simple as that: `1`, so it return a single integer to you. – user4003407 Jun 29 '15 at 02:51
  • @PetSerAl Yes, it does. The last line on my post was correct; I switched `$arg` to `$args` and it returned data. – rob Jun 30 '15 at 20:08

2 Answers2

0

Pay attention to your function. It gets an open curly brace '{', but not the matching close curly brace '}'. Your defective PowerShell code will emit an error not caught by your code.

After you correct this simple error, notice how you're calling your function. What is $arg? I assure you it's not any automatic variable. Have a look in about_automatic_variables...

P.S.: you'd better off asking enormously difficult questions like this one in social . technet . microsoft . com / Forums / windowsserver / en-US / home?forum=winserverpowershell . If you ha did it, the answer would have been posted many hours ago.

  • The part about `$arg` was one of the errors. In the script, I had to use `$args`. – rob Jun 30 '15 at 20:04
0

The problem is that "return" is not what you think.

In Powershell, the "return value" is the last value on the stack when execution ends. In your case, just omit the "return" keyword, and the string will come out as you expect.

Function return value in PowerShell

Alternatively, you can use Write-Output which would explicitly send the data to the output like a C-style return statement.

Note: Do NOT use Write-Host, as it writes directly to the powershell host, skipping the pipeline and never giving you a chance to see the value.

Community
  • 1
  • 1
Eris
  • 7,378
  • 1
  • 30
  • 45