1

Maybe i need a pair of fresh eyes on that:

i use the following code in a .bat file in order to call a Powershell script which accepts 2 parameters.And i want to pass the same parameters in a function which is the Powershell script.The problem is that i cannot seperate the 2 parameters inside the function.Here is the code inside the .bat file :

@ECHO OFf
Set OUTFILE="C:\users\XXX\desktop\newchangepass\log.txt"

echo user='%1' pass='%2' >> %OUTFILE%

echo %1 %2

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -File "C:\ps\CallMeWithParams.ps1" %1 %2 

That's what i give to the command line:

c:\PS>CallMeWithParams.bat Alpha Beta

and here is the code inside the Powershell script:

param ([Parameter (Mandatory)]$param1, [Parameter(Mandatory)]$param2 )
function foo{
    param ($fusername,$fpassword)
    write-output $fusername

}
foo($param1,$param2)

The output which i receive obviously in CMD is:

c:\PS>CallMeWithParams.bat Alpha Beta
Alpha Beta
Alpha
Beta

i should get only the first parameter....

The code inside the PS script is there for test reasons.I want to be sure that i get the two parameters and that i can seperate them in order to use the second one in an update statement.

Any suggestion would be greatly appreciated. PS1: Yes the CMD arguments will be enclosed in double quotes PS2: atm there is no control on variable types etc. I wanted to keep it simple for debugging reasons.

amon
  • 93
  • 1
  • 6
  • 3
    Change `foo($param1,$param2)` to `foo -fusername $param1 -fpassword $param2` in the last line of the script – Mathias R. Jessen Mar 19 '21 at 14:25
  • Mathias is right - the reason is `$fpassword` is not mandatory, so you're just running `foo` twice with the first parameter only. The parentheses creates a single list object – Cpt.Whale Mar 19 '21 at 14:43
  • @user19702 No, the code calls `foo` only once, but passes an array consisting of `$param1` and `$param2` for parameter `-fusername`. – zett42 Mar 19 '21 at 15:27
  • 1
    Does this answer your question? [How do I pass multiple parameters into a function in PowerShell?](https://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell) – zett42 Mar 19 '21 at 15:34
  • Thank you very much Mathias, that was it! – amon Mar 23 '21 at 10:04

0 Answers0