首页 > 解决方案 > 如何使用 Runtime.exec() 发送的“Get-VpnConnection”PowerShell 命令获取 VPN 的 ConnectionStatus?

问题描述

在我的 Java 代码中,我想确保在通过 PowerShell 命令添加 VPN 并调用 rasdial 连接到它之后,它实际上是连接的。

Runtime rt = Runtime.getRuntime();                  
try {                   
    String command = "powershell.exe Add-VpnConnection -Name \"ProxyConnector\" -ServerAddress \""+serverAddress+":"+port+"\" ";
    rt.exec(command);
    String command2 = "rasdial.exe ProxyConnector "+username+" "+password;
    rt.exec(command2);                          
} catch (IOException e) {
    e.printStackTrace();
}

我知道该命令Get-VpnConnection -Name %VpnName%提供了有关 VPN 的所有信息(如果存在),但我如何只取值 ConnectionStatus?它对我的目的真的可靠吗?

标签: javapowershellvpnruntime.exec

解决方案


推荐阅读