Batch – Random IT Utensils https://blog.adamfurmanek.pl IT, operating systems, maths, and more. Sat, 02 Jan 2021 19:08:39 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Availability Anywhere Part 2 — Connecting to VPN automatically in Windows https://blog.adamfurmanek.pl/2019/11/30/availability-anywhere-part-2/ https://blog.adamfurmanek.pl/2019/11/30/availability-anywhere-part-2/#comments Sat, 30 Nov 2019 09:00:49 +0000 https://blog.adamfurmanek.pl/?p=3174 Continue reading Availability Anywhere Part 2 — Connecting to VPN automatically in Windows]]>

This is the second part of the Availability Anywhere series. For your convenience you can find other parts in the table of contents in Part 1 – Connecting to SSH tunnel automatically in Windows

Last week we saw how to configure automatic SSH tunnel connection. Today we will go with VPN.

I am using built-in VPN provider (SSTP and L2TP) supported by rasdial.

First, configure your VPN (using either PS or GUI). It doesn’t matter which options you choose, just make sure it works and can connect correctly.
Next, it’s time to set up a script for automatically connecting to VPN:

ping -n 1 YOUR_VPN_SERVER_IP | find "time=" > NUL
if not errorlevel 1 (
	netsh interface IPv4 set dnsserver "Wi-Fi" static 0.0.0.0 both
	netsh interface IPv4 set dnsserver "Local Area Connection" static 0.0.0.0 both
	exit
)

rasdial VPN_NAME /disconnect
netsh interface IPv4 set dnsserver "Wi-Fi" source="dhcp"
netsh interface IPv4 set dnsserver "Local Area Connection" source="dhcp"

rasdial VPN_NAME USERNAME PASSWORD

ping -n 1 YOUR_VPN_SERVER_IP | find "time=" > NUL
if not errorlevel 1 (
	netsh interface IPv4 set dnsserver "Wi-Fi" static 0.0.0.0 both
	netsh interface IPv4 set dnsserver "Local Area Connection" static 0.0.0.0 both
)

This script does multiple things. First, in lines 1 to 6 I check if I am already connected. I am running this script frequently (each minute) and I want to disable DNS leaking so I set DNS Server to invalid address for regular network interfaces when I’m connected. If everything is okay, I exit in line 5.

If I cannot reach the server then I disconnect (line 8), I restore DHCP for DNS resolution and then in line 12 I try connecting. Finally, I check if I’m connected and then reset DNS again.

This configuration works for “regular” networks (like at home or in my office) and also for those hotel WiFis requiring authentication via browser. If it detects it cannot connect to VPN server then it resets settings to DHCP and then I can authenticate in the browser.

Now, you need to have task for task scheduler. This is almost the same as last week only this time I’m running it as a user in which I log into the machine. Just keep in mind that when you change your password then you need to go to task scheduler, open settings of the task, close them and enter new password, then restart your computer.

]]>
https://blog.adamfurmanek.pl/2019/11/30/availability-anywhere-part-2/feed/ 1
Capturing all output in Powershell https://blog.adamfurmanek.pl/2019/10/05/capturing-all-output-in-powershell/ https://blog.adamfurmanek.pl/2019/10/05/capturing-all-output-in-powershell/#respond Sat, 05 Oct 2019 08:00:56 +0000 https://blog.adamfurmanek.pl/?p=3143 Continue reading Capturing all output in Powershell]]> Today very simple trick to capture whole output from Powershell. Let’s see those files:

1.ps1

echo "1_1 STD OUT"
write-host "1_2 HOST OUT"
write-error "1_3 STD ERR"

.\2.ps1

2.ps1

echo "2_1 STD OUT"
write-host "2_2 HOST OUT"
write-error "2_3 STD ERR"

.\3.bat

3.bat

echo "3_1 STD OUT"
echo "3_2 STD ERR" 1>&2
echo "3_2 STD 3" 1>&3

cscript 4.vbs

4.vbs

Wscript.Echo "4_1 STD OUT"
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
Set stderr = fso.GetStandardStream (2)
stdout.WriteLine "4_2 STD OUT"
stderr.WriteLine "4_3 STD ERR"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec("5.exe")
WScript.StdOut.Write WshShellExec.StdOut.ReadAll
WScript.StdErr.Write WshShellExec.StdErr.ReadAll

5.exe

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << "5_1 STD OUT" << "\n";
    std::cerr << "5_2 STD ERR" << "\n";

    system("6.exe");
}

6.exe

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << "6_1 STD OUT" << "\n";
    std::cerr << "6_2 STD ERR" << "\n";
}

Let’s now run this with powershell:

PS C:\Afish\Playground> .\1.ps1
1_1 STD OUT
1_2 HOST OUT
C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ .\1.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1


C:\Afish\Playground>echo "3_1 STD OUT"
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2
"3_2 STD ERR"

C:\Afish\Playground>echo "3_2 STD 3"  1>&3
"3_2 STD 3"

C:\Afish\Playground>cscript 4.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_2 STD OUT
4_3 STD ERR
5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Okay, we have all STD OUT and STD ERR streams. Let’s now try redirecting this to file:

PS C:\Afish\Playground> .\1.ps1 > out.txt
1_2 HOST OUT
C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ .\1.ps1 > out.txt
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1

"3_2 STD ERR"
4_3 STD ERR
5_2 STD ERR
6_2 STD ERR

File content:

1_1 STD OUT
2_1 STD OUT

C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 

C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_2 STD OUT
5_1 STD OUT
6_1 STD OUT

Okay, so standard output was redirected correctly but standard error and write-host was not. Let’s try this:

PS C:\Afish\Playground> .\1.ps1 2>&1 > out.txt
1_2 HOST OUT
2_2 HOST OUT

File content:

1_1 STD OUT
C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ .\1.ps1 2>&1 > out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1
 
2_1 STD OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1
 

C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 
.\3.bat : "3_2 STD ERR" 
At C:\Afish\Playground\2.ps1:5 char:1
+ .\3.bat
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: ("3_2 STD ERR" :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 

C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_3 STD ERR
4_2 STD OUT


5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Okay, we have almost whole output in the file. Only write-host was not redirected. Can we do better?

The trick is to use CMD:

PS C:\Afish\Playground> cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 > out.txt

File content:

1_1 STD OUT
1_2 HOST OUT
cmd : C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 > out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (C:\Afish\Playground\1.ps1 : 1_3 STD ERR:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

 

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR

At C:\Afish\Playground\1.ps1:5 char:1

+ .\2.ps1

+ ~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1

 


C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 
"3_2 STD ERR" 


C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_2 STD OUT
4_3 STD ERR


5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Now we have everything in file. How to see it in the console as well?

PS C:\Afish\Playground> cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 | Tee-Object -FilePath out.txt
1_1 STD OUT
1_2 HOST OUT
cmd : C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 | Tee-Object -File ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (C:\Afish\Playground\1.ps1 : 1_3 STD ERR:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1


C:\Afish\Playground>echo "3_1 STD OUT"
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2
"3_2 STD ERR"

C:\Afish\Playground>echo "3_2 STD 3"  1>&3
"3_2 STD 3"

C:\Afish\Playground>cscript 4.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_3 STD ERR
4_2 STD OUT
5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

File output:

1_1 STD OUT
1_2 HOST OUT
cmd : C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 | Tee-Object -File ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (C:\Afish\Playground\1.ps1 : 1_3 STD ERR:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

 

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR

At C:\Afish\Playground\1.ps1:5 char:1

+ .\2.ps1

+ ~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1

 


C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 
"3_2 STD ERR" 


C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_3 STD ERR


4_2 STD OUT
5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Now it works correctly.

]]>
https://blog.adamfurmanek.pl/2019/10/05/capturing-all-output-in-powershell/feed/ 0