none
Powershell Script Parameter Issues

    Question

  • I made a little script to help me compile and run some programs from source. However the parameters aren't being set 

    the way I intended them to be:

    Param(
      [Parameter(Mandatory=$True)]
    	[string]$source,
    	[string]$input,
    	[string]$output,
    )
    I'm trying to make the default i/o to standard and that works but for some reason even when i specify the the i/o as a argument for the script the input will remain as null. The output string does take on its parameter though. Any suggestions?
    Sunday, July 14, 2013 3:41 PM

Answers

All replies

  • Please do the following:

    Edit a new, empty script file and paste the above lines of code (your param declaration) into the new, empty script. Next, put some sample code that handles the parameters and just does some sample outputs. Then, give us some sample commands that produce what appears to be incorrect outputs. Finally, please copy and paste the exact output you are getting and why it seems wrong.

    Remember, we can't guess your intent, nor can we see your screen.

    Bill

    Sunday, July 14, 2013 4:10 PM
  • Param( [Parameter(Mandatory=$True)] [string]$source, [string]$input, [string]$output, ) g++ $source if ($input.length -eq 0 -and $output.length -eq 0) { .\a.exe } elseif ($input.length -eq 0) { .\a.exe > output } elseif ($output.length -eq 0) { get-content $input | .\a.exe } else { get-content $input | .\a.exe > output }

    runcpp -source agrinet.cpp -input agrinet.in -output agrinet.out

    should compile and run the agrinet program taking in agrinet.in as input and writinig to agrinet.out.

    However the console still asks for my manual stdin input although it writes output to agrinet.out.

    Monday, July 15, 2013 6:29 PM
  • When I check for the string length of $input I get 0.
    Monday, July 15, 2013 6:32 PM
  • Param( [Parameter(Mandatory=$True)] [string]$source, [string]$input, [string]$output, ) g++ $source if ($input.length -eq 0 -and $output.length -eq 0) { .\a.exe } elseif ($input.length -eq 0) { .\a.exe > output } elseif ($output.length -eq 0) { get-content $input | .\a.exe } else { get-content $input | .\a.exe > output }

    runcpp -source agrinet.cpp -input agrinet.in -output agrinet.out

    should compile and run the agrinet program taking in agrinet.in as input and writinig to agrinet.out.

    However the console still asks for my manual stdin input although it writes output to agrinet.out.

    What you have posted does not have much to do with theissue you are having. The variables have nothing to do with hte program you are running. Why is it you think they are related?

    What is RUNCPP?  Is it a program?  Is it a batch file?  How does it relate to the bit of script you have posted?


    ¯\_(ツ)_/¯

    Monday, July 15, 2013 6:41 PM
  • It doesnt but the moderator asked me for it. runcpp is the name of the script I am trying to call with the parameters that are giving me this issue. 
    Monday, July 15, 2013 6:47 PM
  • It is the script that I pasted.
    Monday, July 15, 2013 6:47 PM
  • The script needs to be run like this:

    Either:

    .\runcpp.ps1 -source agrinet.cpp -input agrinet.in -output agrinet.out

    Or:

    <path to>\runcpp.ps1 -source agrinet.cpp -input agrinet.in -output agrinet.out

    Prompts  from "a.exe" are not the domain of the script or this forum.


    ¯\_(ツ)_/¯

    Monday, July 15, 2013 6:52 PM
  • Hi,

    remove  the >,< after $Output and use a other Variablename for $Input, $Input is a System variable,

    enter

    cd variable:

    dir

    Yous see it list you a varaible with the Name input 


    see the difference

    function foo {
    	Param(
    		[Parameter(Mandatory=$True)]
    		[string]$source,
    		[string]$MyInput,
    		[string]$output
    	)
    	Write-Host "Parameter `$source contain $source" 
    	Write-Host "Parameter `$MyInput contain $MyInput" 
    	Write-Host "Parameter `$output contain $output" 
    }
    
    foo -source C:\TEST -MyInput D:\ABC -output E:\123

    Parameter $source contain C:\TEST
    Parameter $MyInput contain D:\ABC
    Parameter $output contain E:\123

    function foo {
    	Param(
    		[Parameter(Mandatory=$True)]
    		[string]$source,
    		[string]$Input,
    		[string]$output
    	)
    	Write-Host "Parameter `$source contain $source" 
    	Write-Host "Parameter `$input contain $input" 
    	Write-Host "Parameter `$output contain $output" 
    }
    
    foo -source C:\TEST -Input D:\ABC -output E:\123

    Parameter $source contain C:\TEST
    Parameter $input contain
    Parameter $output contain E:\123
    • Edited by brima Monday, July 15, 2013 7:11 PM add code
    Monday, July 15, 2013 7:05 PM
  • So I tried it the first bolded way but the script still doesn't recognize the -input paramater.

    $input.length is returning zero which doesn't make sense.

    Monday, July 15, 2013 7:11 PM
  • check my posting, use a other Name for Input !
    Monday, July 15, 2013 7:14 PM
  • Right on, it works!

    Thank you sir/ma'am

    Monday, July 15, 2013 7:17 PM
  • check my posting, use a other Name for Input !

    Good call.  $input is reserved although I do not think it is the whole issue here.

    ¯\_(ツ)_/¯

    Monday, July 15, 2013 7:18 PM
  • then  >Please click “Mark as Answer” in my posting!

    Thank you.

    Best regards
    brima

    Monday, July 15, 2013 7:25 PM