0

I have a question, I have a 2 scripts lets name then first.ps1 and second.ps1 and they look something like

first.ps1:

param(
    [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$filename="",
    [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$arguments=""
)

Function WriteFirst
{
    Write-Host $filename
    Write-Host $arguments
}

and second.ps1

param(
    [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$log=""
)

Function WriteSecond
{
    Write-Host $log
}

Now using c# (using embedded resource, nothing special) I want to join those two scripts into one, as lets say they can be separate modules from witch I can construct one final script for example first script will execute command and second will read console buffer for output.

Now question is about the pram, do I need to join then somehow or will they add up to itself or maybe override ??

Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87

1 Answers1

3

The way that you're currently setting up the PowerShell scripts is to set the parameters at the script level, and then use them in the function.

A better approach would be to have the parameters at the function level.

param(
    [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$filename="",
    [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$arguments=""
)

Function WriteFirst
{
    param(
        [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$filename="",
        [parameter(Mandatory=$True,ValueFromPipeline=$True)][string]$arguments=""
    )

    Write-Host $filename
    Write-Host $arguments
}

WriteFirst -filename $filename -arguments $arguments

In terms of calling those functions from c#, check out this post. From it, you can infer that powershell.AddScript(...).AddParameter(...) will get you what you're asking for but I believe the powershell.AddCommand(...).AddParameter(...) is a better approach.

G42
  • 9,791
  • 2
  • 19
  • 34