Pass Powershell Variables to Command Prompt line

866 Views Asked by At

I want to make a PowerShell script that can be used to connect computers to various client's SonicWall VPNs (specifically through Global VPN and NetExtender). I would like to have it be like a user interface to prompt the user (which will set the variables) and then use that information to pass through to command lines in the command prompt.

I want to be able to have information entered in be applied in the cmd line in the script.

I have tried using the MobileConnect connection through (Using the the app from the app store) and connecting with the Microsoft VPN client, but that does not grab all the network information; specifically DNS servers.

The best way is to install either Global VPN or NetExtender and connect through cmd line; that way will grab the entire network information.

This is the basic command to run it:

 Function Connect-VPN {

 Set-Location -Path "C:\Program Files (x86)\SonicWALL\SSL-VPN\NetExtender"

 cmd /c "NECLI connect -s address:4433 -u Uname -p Password -d Domain -A"

 Set-Location -Path C:\

 }

Basically, you change the directory and execute the commands with those arguments.

I would like to prompt in POSH, create the variables with the user input, then have those arguments passed down.

What I have right now is:

param(
  [string]$Testadd ,
  [string]$Testun ,
  [string]$TestPW ,
  [string]$TestDom 
  )


If ($Testadd -eq "")
   {$Testadd = (Read-Host "test")
   }

If ($Testun -eq "")
   {$Testun = (Read-Host "test")
   }


If ($TestPW -eq "")
   {$TestPW = (Read-Host "test")
   }


If ($TestDom -eq "")
   {$TestDom = (Read-Host "test")
   }

 Set-Location -Path "C:\Program Files (x86)\SonicWALL\SSL-VPN\NetExtender"

 cmd /c "NECLI connect -s "$($Testadd)" -u "$($Testun)" -p "$($TestPW)" -d "$($TestDom)" -A"

 Set-Location -Path C:\

The problem is that the all the arguments come out null. I do not know if it is possible, but I wanted to see.

3

There are 3 best solutions below

1
On BEST ANSWER

You can try to build the string before running the cmd

param (
    [string]$Testadd,
    [string]$Testun,
    [string]$TestPW,
    [string]$TestDom
)


If ($Testadd -eq "")
{
    $Testadd = (Read-Host "testadd")
}

If ($Testun -eq "")
{
    $Testun = (Read-Host "testun")
}


If ($TestPW -eq "")
{
    $TestPW = (Read-Host "testpw")
}


If ($TestDom -eq "")
{
    $TestDom = (Read-Host "testdom")
}

Set-Location -Path "C:\Program Files (x86)\SonicWALL\SSL-VPN\NetExtender"

#build the string before
$cmd = "NECLI connect -s " + $($Testadd) + " -u " + $($Testun) + " -p " + $($TestPW) + " -d " + $($TestDom) + " -A"

# Or even like this
$cmd = "NECLI connect -s $Testadd -u $Testun -p $TestPW -d $TestDom -A"

# exec command
cmd /c $cmd

Set-Location -Path C:\

0
On

Calling external exe / commands that use cmd.exe, require special consideration and outing specifics. You also do not need to call cmd.exe directly, as that will just happen. This is a well documented this. For example:

PowerShell: Running Executables

  1. The Call Operator &

Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.

In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.

The PowerShell V3.0 parser do it now smarter, in this case you don’t need the & anymore .

Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command,it cannot interpret command parameters

 # Example: 

& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi"  /fullscreen
  1. Start-Process (start/saps)

Why: Starts a process and returns the .Net process object Jump if -PassThru is provided. It also allows you to control the environment in which the process is started (user profile, output redirection etc). You can also use the Verb parameter (right click on a file, that list of actions) so thatyou can, for example, play a wav file.

Details: Executes a program returning the process object of the application. Allows you to control the action on a file (verb mentioned above) and control the environment in which the app is run. You also have the ability to wait on the processto end. You can also subscribe to the processes Exited event.

#Example: 

#starts a process, waits for it to finish and then checks the exit code.

$p = Start-Process ping -ArgumentList "invalidhost" -wait -NoNewWindow -PassThru

$p.HasExited

$p.ExitCode


10. Stop-Parsing Symbol --%

Why: Its a quick way to handle program arguments that are not standard. Also its the new cool way to do it.

 Details: The stop-parsing symbol (--%), introduced in Windows PowerShell 3.0, directs Windows PowerShell to refrain from interpreting input as Windows PowerShell commands or expressions. When calling an executable program in Windows PowerShell, placethe stop-parsing symbol before the program arguments.

 After the stop-parsing symbol --% , the arguments up to the end of the line (or pipe, if you are piping) are passed as is.

#Examples:

# icacls in V2

# You must  use escape characters to prevent PowerShell from misinterpreting the parentheses.

icacls X:\VMS /grant Dom\HVAdmin:`(CI`)`(OI`)F



# In V3 you can use the stop-parsing symbol. 
icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F

See also:

Using Windows PowerShell to run old command line tools (and their weirdest parameters)

Solve Problems with External Command Lines in PowerShell

Quoting

About Quoting Rules

A Story of PowerShell Quoting Rules

0
On

To add to @Desinternauta, I suspect it is how the command is interpreting the quotes and the variables. i.e. when you write out the string as you have it, it adds spaces:

$b = "b"
Write-Host "a"$($b)"c"

Outputs:

a b c

The good news is that double quoted strings allow you to embed the variables into the string:

cmd /c "NECLI connect -s $Testadd -u $Testun -p $TestPW -d $TestDom -A"