D
D
Dmitry Shumov2018-04-05 21:45:07
PowerShell
Dmitry Shumov, 2018-04-05 21:45:07

Why doesn't RemoteApp connect from under PowerShell?

Good evening!
There is an idea for a script that does the following:
1) Creates a VPN connection
2) Connects via VPN
3) Starts RemoteApp
4) Waits for RemoteApp to complete / close
5) Breaks the VPN connection
6) Deletes the VPN connection.
Here is the script:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Add-VpnConnection -Name VTH_VPN -ServerAddress 1.1.1.1 -AllUserConnection -AuthenticationMethod MSChapv2 -PassThru -RememberCredential -TunnelType Pptp
rasdial VTH_VPN login password 
[Console]::outputEncoding =[System.Text.Encoding]::GetEncoding('cp866')
$p = start-process C:\VPN\vth.rdp -PassThru
$p.WaitForExit()
rasdial VTH_VPN /disconnect
Remove-VpnConnection -Name VTH_VPN -AllUserConnection -Force

What happens:
1) Creates a VPN connection
2) Connects via VPN
3) Tries to start RemoteApp, but at the same time breaks the VPN connection and deletes it.
Accordingly, by the time you try to launch RemoteApp, the VPN no longer exists, and the launch fails.
For debugging, I decided to replace RemoteApp with the calc.exe application - the result is the same. BUT, if I replace calc.exe with notepad.exe or just with mstsc.exe the script works with a bang.
Tell me please, where is the plug? Or is the approach fundamentally wrong?
I understand that you will say that it is not safe to pass login and password in the script - I know. But I plan to pack the script into an exe file later. So more or less a little more secure.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shumov, 2018-04-06
@dshumov

The question is closed. Helped on technet. The solution is as follows (if anyone is interested): in addition to mstsc, the wksprt.exe process (Runtime environment for connections to RemoteApp and remote desktops) is also launched, and it was necessary to check for wksprt.exe, because. apparently mstsc launches it and starts a new process, the final script turns out like this:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Add-VpnConnection -Name VTH_VPN -ServerAddress 1.1.1.1 -AllUserConnection -AuthenticationMethod MSChapv2 -PassThru -RememberCredential -TunnelType Pptp
rasdial VTH_VPN login password 
[Console]::outputEncoding =[System.Text.Encoding]::GetEncoding('cp866')
Start-Process mstsc -arg C:\VPN\vth.rdp -NoNewWindow -Wait
while (gps wksprt,mstsc -ea 0) {
  Start-Sleep -Sec 1
}
rasdial VTH_VPN /disconnect
Remove-VpnConnection -Name VTH_VPN -AllUserConnection -Force

A
azarij, 2018-04-05
@azarij

try replacing:
$p = start-process C:\VPN\vth.rdp -PassThru
$p.WaitForExit()
with:
start-process mstsc.exe -argumentlist "c:\vpn\vth.rdp" -wait
-passthru might needed, or maybe not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question