Windows remote connection looping MSTSC - windows

I built a program that will connect to a list of remote machines through Windows. The only issue I'm having is that after it connects to the Remote Desktop the Windows command prompt is in a "frozen state" until I disconnect from the remote. At the point it loops the code back where you can select another machine.
I want it to be able to loop back without having to disconnect so I can connect to multiple machines.
Here is my code:
:deployCode
cmdkey /generic:"SERVERNAME" /user:"user" /pass:"password"
mstsc /v:"SERVERNAME"
cls
goto :machinelist
It doesn't step through the cls clear command and go back to my machine list (goto :machinelist) until I close the server I connect to.
I tried using "&" to join the two commands but that did not work.
i.e mstsc /v:"SERVERNAME" & goto :machinelist
How can I step to cls and goto :machinelist without having to disconnect after mstsc?

mstsc does not return if within a script. Prefix by "start" it will work (just tested here)
:deployCode
cmdkey /generic:"SERVERNAME" /user:"user" /pass:"password"
start mstsc /v:"SERVERNAME"
cls
goto :machinelist

Related

Automating connection to VNC with batch file

I'm trying to create a batch file to automate vnc connections, this is what i came up with:
#echo off
:Begin
set "PASS=123"
set /p IP=Enter IP Address:
echo Connecting...
start /d "C:\Program Files (x86)\UltraVNC\" VNCVIEWER.EXE %IP%
goto Begin
problem is that the program comes up with a second pop up display for the password which is always 123 but i don't know how to make that automatic too, once the process is open how do i make the batch file enter the password as well automatically ?

shortcut (.LNK) is copied from local network to user pc without his icon

I wrote a batch file script in order to copy shortcut from the local network to the user desktop station. everything is working perfect except that the shortcut that is copied to the user desktop is without his icon. I have no experience writing batch files, i will be grateful if someone can advice me how to solve that issue.
that is the script:
#echo off
setlocal
set alias=Reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP"
FOR /F "TOKENS=6 DELIMS=\." %%A IN ('%alias%') DO set .NetVer=%%A
if [%.NetVer] ==[] GOTO noDotNet
if EXIST C:\Users\%USERNAME%\Desktop\Partner_Projects_System.lnk GOTO ShortCutAllreadyExist
if not exist "\\netapp2\public\all\INTERNET PROJECTS\PMS_Shortcut_noWhite\Partner_Projects_System.lnk" GOTO noPermissionToDir
copy "\\netapp2\public\all\INTERNET PROJECTS\PMS_Shortcut_noWhite\Partner_Projects_System.lnk" "C:\Users\%USERNAME%\Desktop"
:loop
ping -n 1 -w 3000 1.1.1.1 > nul
IF EXIST C:\Users\%USERNAME%\Desktop\Partner_Projects_System.lnk (
GOTO startApp
) ELSE (
GOTO loop
)
EXIT
pause
:noDotNet
msg "%username%" DOTNET Framework is not installed on your machine please contact partner helpdesk team
EXIT
:noPermissionToDir
msg "%username%" you need to have read and write permission to the path \\netapp2\public\all\INTERNET PROJECTS\ please contact partner helpdesk team
EXIT
:ShortCutAllreadyExist
msg "%username%" the shortcut is allredy exist on the desktop
EXIT
:startApp
msg "%username%" SHORTCUT TO PROJECT MANAGMENT SOFTWARE WAS CREATED ON YOUR DESKTOP
start C:\Users\%USERNAME%\Desktop\Partner_Projects_System.lnk
A shortcut can specify a separate path to a icon that is different from the actual shortcut target. If this path is not valid on the new machine the icon might not display correctly.
You cannot manipulate shortcuts with a batch file but you can with Windows Scripting Host. Try this script to print the shortcut properties.

Selenium hover action with IE doesn't work if RDP never connected to Jenkins slaves

We create a bunch of windows Jenkins slaves on Amazon EC2, and make them autologon after they are provisioned.
Once logged on, we use connect the slaves back to master via java web start by execute "ps", with all of this, we can run selenium perfectly with IE except we can't get a higher resolution than 1024*768. We got screenshot, we can see the browser running on the GUI if connected with RDP, just like running on a physical PC.
But a flaw is that, the hover action doesn't work on IE unless we connect to the slave through remote desktop one time before the test starts, just connect once, nothing else, I can even close the RDP session.
I guess something triggered by the RDP connection, just can't figure out what is that.
You can try to adapt your test scripts to have them make an RDP connection before you start your tests, if there is none. This is the code I used for one of our projects to ensure an RDP connection is active:
REM check if user has an RDP session. Will store the session ID of the user in the SESSION variable
call :getsession computername username SESSION
if "%SESSION%" EQU "" (
REM initiate an RDP connection to 'computername', wait, and disconnect by killing the process
mstsc.exe /v:computername & ping -n 5 1.1.1.1 & taskkill /F /IM mstsc.exe
)
REM check again the session
call :getsession computername user SESSION
if %SESSION% NEQ "" (
echo start your tests
)
:getsession
for /F "tokens=1,2,3,4" %%i in ('query session /SERVER:%1 ^| findstr %2') do if %%j EQU %2 (SET %3=%%k) else (SET %3=%%j)
goto :eof
Note that the automatic connection will only be made if you save credentials on the machine from which you start the remote desktop:
cmdkey /generic:computername /user:rdp_username /pass:rdp_password
After executing this once the mstsc.exe will be able to connect to the machine without user interaction.

Stuck in cmd script loop

I'm using a script that, upon logging in, loads Remote Desktop Protocol (RDP) and pushes the user to a Virtual Desktop Environment (VDI), along with their credentials. The script does not allow the next command 'Shutdown /l' to run until it is finished running.
The problem I'm having is that once in the VDI, RDP tries to load up again. How do I prevent this from happening? Thank you in advanced.
start /wait \\ph-vda\C$\VDI_Users_RDP\%username%.rdp /multimon /noConsentPrompt
Shutdown /l
I think what you're describing is that the same script is running when the user logs in, even when they log into the VDI. You don't want the script to run in the VDI. If that's right, here's one idea.
#echo off
if exist "%userprofile%\in_vdi" goto :eof
type nul >"%userprofile%\in_vdi"
start /wait \\ph-vda\C$\VDI_Users_RDP\%username%.rdp /multimon /noConsentPrompt
del "%userprofile%\in_vdi"
Shutdown /l

Windows batch file using start not placing multiple programs in background

I am trying to get data from a sql server and ldap server for multiple clients. I need to get the sql data first and then the ldap data. In Unix shell it was straight forward to make a loop around a sub process with both retrievals going on for each client and then wait for it to complete. As a windows batch file however it happens sequentially. I.e. until I retrieve the data for one client, it won't go to the next. How can I get each client's data simultaneously? Here's what I have:
REM Get DB and client info. from config file
for /F "tokens=1,2,3,4,5,6 delims=| eol=#" %%G in (%cfg%\%env%.data) do (
REM Mark file as being in process of receiving data
type nul > %%K.tmp
REM Get data and remove tmp file to indicate completion
start cmd /C sqlcmd .... -Q "A long query" ^> %%K.dat1 && "c:\Program Files\Softerra\LDAP Administrator 4\laimex.exe" ... /sql "Another query" > %%K.dat2 && del %%K.tmp
)
For some reason, I need to do the first redirect escaped as ^> while the later one doesn't need that. At this point I am assuming that everything will be retrieved in the background and that I will need to check afterwards for when the processes are complete which I would do by checking the existence of the zero byte temp files I create. What happens though is that each iteration through the loop only starts when the prior one completes rather than occurring straight away by being placed in the background. Can anyone suggest how I can fix this?
You need to escape the && as well (^&^&), otherwise it executes everything after it as soon as start is fired. Example:
1 gets executed in a new shell correctly, while 2 takes over the main window (not what you want).
start cmd /C ping 127.0.0.1 && ping 127.0.0.2
Both get executed one after the other in a new window.
start cmd /C ping 127.0.0.1 ^&^& ping 127.0.0.2
Same as above, another way to do it.
start cmd /C "ping 127.0.0.1 && ping 127.0.0.2"
Also escape the other >'s, this might work:
start cmd /C sqlcmd .... -Q "A long query" ^> %%K.dat1 ^&^& "c:\Program Files\Softerra\LDAP Administrator 4\laimex.exe" ... /sql "Another query" ^> %%K.dat2 ^&^& del %%K.tmp

Resources