将 Powershell 变量传递到命令提示符行

Pass Powershell Variables to Command Prompt line

我想制作一个 PowerShell 脚本,可用于将计算机连接到各种客户端的 SonicWall VPN(特别是通过 Global VPN 和 NetExtender)。我想让它像用户界面一样提示用户(这将设置变量),然后使用该信息传递到命令提示符中的命令行。

我希望能够在脚本的 cmd 行中应用输入的信息。

我已尝试通过(使用应用商店中的应用)使用 MobileConnect 连接并与 Microsoft VPN 客户端连接,但这并没有获取所有网络信息;特别是 DNS 服务器。

最好的方法是安装 Global VPN 或 NetExtender 并通过 cmd 行连接;这样就可以抓取全网信息了。

这是运行它的基本命令:

 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:\

 }

基本上,您更改目录并使用这些参数执行命令。

我想在 POSH 中提示,使用用户输入创建变量,然后传递这些参数。

我现在拥有的是:

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:\

问题是所有参数都为空。不知道可不可以,我想看看

您可以尝试在 运行 命令

之前构建字符串
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:\

添加到@Desinternauta,我怀疑命令是如何解释引号和变量的。即,当您按原样写出字符串时,它会添加空格:

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

输出:

a b c

好消息是双引号字符串允许您将变量嵌入到字符串中:

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

调用使用 cmd.exe 的外部 exe / 命令需要特殊考虑和外出细节。您也不需要直接调用 cmd.exe,因为那会发生。这是一个有据可查的。例如:

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

另请参阅:

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

Solve Problems with External Command Lines in PowerShell

引用

About Quoting Rules

A Story of PowerShell Quoting Rules