3 Replies - 144 Views - Last Post: 04 December 2019 - 08:14 AM Rate Topic: -----

#1 TerraEnvy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 07-November 08

Powershell Pass Variable into Arguments

Posted 04 December 2019 - 07:36 AM

Hello Everyone!
I am looking for some assistance in what I may be doing wrong in this script.

The script itself is well over 1500 lines and setup in functions and only this one function does not work.

I work for a company that uses ingenico hardware and it has an installer that has a gui for it, but when dealing with as many computers as I do it is just not feasible.

Here the function I am working with that I just can not get to work and need more eyes on after trying for 3 days.



This is the default code that is used by the Driver installer and this command WORKS:
Start-Process -FilePath "IngenicoUSBDrivers_setup.exe" -Verb runAs -ArgumentList '/S' , '/v "/qn"' , '/PORT=19' , '/PID=0060:19'


However the PORT and PID changes based on the computer.
I tried storing this in variables and it does not seem to ever work, or even launch the executable.

Here is an example of my attempted code:
$PIDN = "0060
$PIDP = "19"

Start-Process -FilePath "IngenicoUSBDrivers_setup.exe" -Verb runAs -ArgumentList '/S' , '/v "/qn"' , '/PORT=$PIDP' , '/PID=$PIDN:$PIDP'


I have also tried modifying the PID argument as well so the whole thing looks like:
$PIDN = "0060
$PIDP = "19"

Start-Process -FilePath "IngenicoUSBDrivers_setup.exe" -Verb runAs -ArgumentList '/S' , '/v "/qn"' , '/PORT=$PIDP' , '/PID=$PIDN":"$PIDP'



Any assistance with this one would be very helpful.

Document with command line information for this software can be found at:
https://developer.in...erUserGuide.pdf

Edit:
I have now pulled this function out into it's own script and tried with the same results.
Also have tried using a write-host to make sure it is populating correctly, and the following DOES put the text in proper format and spacing
$PIDN = "0060
$PIDP = "19"
$PIDTest = $PIDN":"$PIDP
write-host $PIDN":"$PIDP <# SHOWS CORRECTLY#>

Start-Process -FilePath "IngenicoUSBDrivers_setup.exe" -Verb runAs -ArgumentList '/S' , '/v "/qn"' , '/PORT=$PIDP' , '/PID=$PIDTest' <# Still doesn't work#>


-Terra

This post has been edited by TerraEnvy: 04 December 2019 - 07:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Powershell Pass Variable into Arguments

#2 no2pencil   User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,520
  • Joined: 10-May 07

Re: Powershell Pass Variable into Arguments

Posted 04 December 2019 - 07:44 AM

Why does the 2nd argument have two slash options? There is a single quote, slash V, double quote, slash QN, double quote, single quote.

None of the other arguments are this way. Is this correct?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Powershell Pass Variable into Arguments

Posted 04 December 2019 - 07:52 AM

Looks like you forgot a quote:
# $PIDN = "0060
$PIDN = "0060"
$PIDP = "19"



Use an editor with syntax highlighting, that stuff should just jump right out.

The list isn't getting escaped the way you expect it is:
$PIDN = "0060"
$PIDP = "19"
$args = '/S' , '/v "/qn"' , '/PORT=$PIDP' , '/PID=$PIDN:$PIDP'
$args



Result:
/S
/v "/qn"
/PORT=$PIDP
/PID=$PIDN:$PIDP



This first one is easy: "/PORT=$PIDP". The second is a little tricky:
+ $args = '/S' , '/v "/qn"' , "/PORT=$PIDP" , "/PID=$PIDN:$PIDP"
+                                                   ~~~~~~
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.



So:
$PIDN = "0060"
$PIDP = "19"
$args = '/S' , '/v "/qn"' , "/PORT=$PIDP" , "/PID=${PIDN}:${PIDP}"
# $args
Start-Process -FilePath "IngenicoUSBDrivers_setup.exe" -Verb runAs -ArgumentList $args


This post has been edited by baavgai: 04 December 2019 - 07:53 AM

Was This Post Helpful? 1
  • +
  • -

#4 TerraEnvy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 07-November 08

Re: Powershell Pass Variable into Arguments

Posted 04 December 2019 - 08:14 AM

View Postbaavgai, on 04 December 2019 - 07:52 AM, said:

Looks like you forgot a quote:
# $PIDN = "0060
$PIDN = "0060"
$PIDP = "19"



Use an editor with syntax highlighting, that stuff should just jump right out.

The list isn't getting escaped the way you expect it is:
$PIDN = "0060"
$PIDP = "19"
$args = '/S' , '/v "/qn"' , '/PORT=$PIDP' , '/PID=$PIDN:$PIDP'
$args



Result:
/S
/v "/qn"
/PORT=$PIDP
/PID=$PIDN:$PIDP



This first one is easy: "/PORT=$PIDP". The second is a little tricky:
+ $args = '/S' , '/v "/qn"' , "/PORT=$PIDP" , "/PID=$PIDN:$PIDP"
+                                                   ~~~~~~
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.



So:
$PIDN = "0060"
$PIDP = "19"
$args = '/S' , '/v "/qn"' , "/PORT=$PIDP" , "/PID=${PIDN}:${PIDP}"
# $args
Start-Process -FilePath "IngenicoUSBDrivers_setup.exe" -Verb runAs -ArgumentList $args




Yeah, the forgotten quote was a mistype when moving it over to DIC.

As for the rest, I feel like an idiot for forgetting passing the arguments in their entirety to a variable.
Thank you very much, this works perfectly!
I have learned something new from the way you handled the second variable in this and is something I will make sure to remember for things in the future and will research the way that functions to understand it better.

-Terra
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1