Is there a problem with passing variable to command-prompt from powershell?

Viewed 162

I have a script for stress testing networks in batch, and it has been working fine for me and doing exactly what I wanted it to do. I recently tried to remake it in powershell, and overall it was the same, except for one detail. Whenever I tried to start new command prompts with start cmd.exe -argumentlist {/k ping $p /l $Bytes /t} -windowstyle Minimized, it starts cmd, but it gets lost at the $p saying Ping request could not find host $p. Please check the name and try again. instead of actually passing the variable. I have been stumped for a while and couldn't find what was causing this issue since when I passed other variables to set the title of the prompts, it worked fine. The variable $p isn't getting undefined either, so I don't know what is causing this.

Here are my scripts

Batch file:

@echo off
title MainPingCenter
(call )
choice /c DC /m "Would you like to ping the default gateway or a custom IP/Web Address?"
IF %ERRORLEVEL% EQU 1 ((for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do (echo Default IP Gateway : %%~a & set i=%%~a))
    goto add)
set /p i="Type in the IP address or website address you want to ping: "

:add
set /p loopcount="How many cmds do you want to ping with? "
set /p Bytes="How many bytes of data do you want to ping with? "
set /a loopcount=loopcount+1

:loop
set /a loopcount=loopcount-1
if %loopcount% LEQ 0 (goto exitloop)
start /min cmd.exe /k ping %i% /l %Bytes% /t
goto loop

:exitloop
echo Success
echo Commands are running in background
pause

:back
choice /c CP /m "Would you like to create more ping cmds or proceed? "
IF %ERRORLEVEL% EQU 1 goto add
IF %ERRORLEVEL% EQU 2 goto choose

:choose
(call )
choice /c YNTC /m "Would you like to close all cmd processes? (Yes, No, Timer, Cancel)"
IF %ERRORLEVEL% EQU 1 goto yes
IF %ERRORLEVEL% EQU 2 goto no
IF %ERRORLEVEL% EQU 3 goto timer
IF %ERRORLEVEL% EQU 4 goto back

:yes
echo Closing all instances of cmd excluding this...
taskkill /im cmd.exe /t /f /fi "windowtitle ne MainPingCenter"
echo Taskkill complete. Press any key to continue...
pause >nul
title Command Prompt
goto :eof

:no
echo Ok, press any key to end this file...
pause >nul
title Command Prompt
goto :eof

:timer
set /p timer="Set amount of seconds until processes are closed: "
choice /c YN /m "Would you ike it to close automatically when the time is finished? "
IF %ERRORLEVEL% EQU 1 (timeout /t %timer% /nobreak & goto yes)
timeout /t %timer% /nobreak
echo Time is up. Press any key to terminate all command prompts
pause >nul
goto yes

Powershell

$host.ui.RawUI.WindowTitle = "Main Ping Center"
while (!($p)) {
    $choice = read-host "Do you want to ping the default gateway, localhost, or a custom address? `n[D,L,C]"
    switch ($choice) {
        "D" {$p = WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value |findstr "{"; $p = $p.trimstart('DefaultIPGateway={"'); $p = $p.trimend('"}'); break}
        "L" {$p = "localhost"; break}
        "C" {$p = read-host "What address do you want to ping?"; break}
    }
    if (!($p)){echo "Invalid input"}
}
$p
while (!($lc -is [int])){
    $lc = read-host "How many cmds do you want to ping with? "
    $ErrorActionPreference = 'SilentlyContinue'
    [int]$lc = $lc
    if (!($lc -is [int])){echo "Invalid input"}
}
while (!($bytes -is [int])){
    $Bytes = read-host "How many bytes of data do you want to ping with? "
    $ErrorActionPreference = 'SilentlyContinue'
    [int]$bytes = $bytes
    if (!($bytes -is [int])){echo "Invalid input"}
}
$ErrorActionPreference = 'continue'
$nametitle = (Get-Random)*([math]::pi)*(Get-Random)
$p
do {
    start cmd.exe -argumentlist {/k title $nametitle `& ping $p /l $Bytes /t} -windowstyle Minimized
    # Variable "$nametitle" gets passed normally but even if I remove the title and "&", $p never gets passed
    $lc--
} until ( $lc -eq 0 )
write "Success`nCommands are running in background"
pause
while (!($C2)) {
    $choice2 = read-host "Would you like to close all cmd processes? (Yes/Y, No/N, Timer/T)"
    switch ($choice2) {
        "Yes" {$C2 = "Yes"}
        "Y" {$C2 = "Yes"}
        "No" {$C2 = "No"}
        "N" {$C2 = "No"}
        "Timer" {$C2 = "Time"}
        "T" {$C2 = "Time"}
    }
    if (!($C2)){echo Invalid input}
}
switch ($C2) {
    "Yes" {echo "Closing all instances of cmd excluding this..."
        taskkill /im cmd.exe /t /f /fi "windowtitle eq $nametitle"
        echo "Taskkill complete. Press any key to continue..."
        pause | out-null
        exit}
    "No" {cd "$home\desktop" 
        echo $nametitle > PingName.txt
        echo "Ok, sending name of ping cmds to text file..."
        echo "Press any key to exit this file..."
        pause | Out-Null
        exit}
    "Time" {$timer = read-host "Set amount of seconds until processes are closed"
        timeout /t $timer /nobreak
        echo "Closing all instances of cmd excluding this..."
        taskkill /im cmd.exe /t /f /fi "windowtitle eq $nametitle"
        echo "Taskkill complete. Press any key to continue..."
        pause | out-null
        exit}
}
0 Answers
Related