2

I have the below PowerShell script

Function Publish
{
    Param(
        [parameter(Mandatory=$true)]
        [String]
        $RELEASEDIR,

        [parameter(Mandatory=$true)]
        [String]
        $SERVICENAME,

        [parameter(Mandatory=$true)]
        [String]
        $SERVER
    )

    Get-ChildItem "$RELEASEDIR\*"
    $service = Get-Service -Name $SERVICENAME -Computername $SERVER -ErrorAction SilentlyContinue
    $service.Status
}
Publish

How I am executing this:

PS C:\Release\RPCPS> .\RPCPublish.ps1 -RELEASEDIR "C:\Location" -SERVICENAME "value" -SERVER "server"
cmdlet Publish at command pipeline position 1
Supply values for the following parameters:
RELEASEDIR:

Even after passing arguments while executing, the script is expecting it again. What am I doing wrong here?

5
  • .\RPCPublish.ps1 =/= your function publish Commented Feb 21, 2017 at 12:25
  • Did not understand Commented Feb 21, 2017 at 12:26
  • dotsource the script and do publish -arg1 -arg2 -arg3, also, consider changing function name to verb-noun format Commented Feb 21, 2017 at 12:27
  • 1
    You script does not accept arguments. Your publish function inside your script does. The arguments you are passing to your script are not getting to your function hence why it starts asking for them. Commented Feb 21, 2017 at 12:27
  • how should I execute it Commented Feb 21, 2017 at 12:30

4 Answers 4

7

If you want to execute the script by calling the .ps1 as in your example, there is no need to use a function. Your script should look just like this:

Param(
    [parameter(Mandatory=$true)]
    [String]
    $RELEASEDIR,
    [parameter(Mandatory=$true)]
    [String]
    $SERVICENAME,
    [parameter(Mandatory=$true)]
    [String]
    $SERVER
    )
Get-ChildItem "$RELEASEDIR\*"
$service = Get-Service -Name $SERVICENAME -Computername $SERVER -ErrorAction SilentlyContinue
$service.Status

The parameters are passed directly to the script and can be used there.

If, on the other hand, you want to establish a (reusable) function, remove just the last line from your script, which calls the function without parameters (which is why it asks for the mandatory parameters every time).

If you remove the last line, you can call the script without parameters once. After that you have a new function Publish in your current session, which you can then call with

Publish -RELEASEDIR "C:\Release\Batchfile" -SERVICENAME "AmazonSSMAgent" -SERVER "10.0.1.91"

independent of the script file.

Sign up to request clarification or add additional context in comments.

2 Comments

How can I execute the same in Jenkins
No idea, I don't do Jenkins. Maybe this will be helpful: hodgkins.io/…
1

Your script is creating a function, "Publish", (lines 1-17) and then calling it with no parameters (line 18). Since you've defined parameters as mandatory (lines 4, 7, 10), failing to supply the parameters when you call the function (line 18) causes PowerShell to request values for the unsupplied parameters.

Supplying parameters to the script file itself does not help; there is no mechanism for "automagically" passing those parameters to anything within the script (you would have to explicitly code the script for that).

As Matt suggested in the comments, dot-source your script after deleting line 18, and then call your function explicitly, passing the parameters (publish -RELEASEDIR "C:\Release\Batchfile" -SERVICENAME "AmazonSSMAgent" -SERVER "10.0.1.91").

Comments

0

As per my understanding your requirement is to run the function, and you have to compile the scripts also in Jenkins.

You can do something like this:

Let's say your script name is RPCPublish.ps1 and the path is D:\Folder.

And I can see your function name is Publish.

So in your case,

powershell -command "& { D:\folder\RPCPublish.ps1; Publish }"

You can pass the parameters after this in the script block.

Comments

0

I used a PowerShell plugin (PowerShell) and executed the same.

. "C:\Release\RPCPS\RPCPublish.ps1"
FUunctionName -RELEASEDIR "C:\bin\Release" -SERVICENAME "Service" -SERVER "$env:SERVER" -DISPLAYNAME "Services Air" -BINPATH "D:\Build\Project.exe" -DESCRIPTION "This service hosts Air service" -DESTINATION "d$\Build\"

2 Comments

Does the plugin have a name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.