programing

Powershell을 사용하여 VPN에 연결

newnotes 2023. 9. 2. 10:12
반응형

Powershell을 사용하여 VPN에 연결

Windows가 로드되는 즉시 VPN 서버에 연결했으면 합니다.파워셸을 사용하여 어떻게 할 수 있습니까?

Windows 10에서 작동합니다.

    $vpnName = "YOUR_VPN_NAME";
    $vpn = Get-VpnConnection -Name $vpnName;
    if($vpn.ConnectionStatus -eq "Disconnected"){
    rasdial $vpnName;
    }

다음과 같은 방법을 사용할 수 있습니다.

저는 그것이 작동하는지 테스트하지 않았습니다.PowerShell V3 베타가 설치되어 있습니다. 이러한 명령을 실행해야 할 수도 있습니다.

Register-ScheduledJob -name ConnectVPN -ScriptBlock { & rasphone MyVpnConnection 
$trigger = New-JobTrigger -AtLogOn
Add-JobTrigger -Name ConnectVPN -Trigger $trigger
Get-ScheduledJob -Name ConnectVPN | Get-JobTrigger

다른 답변 외에도 Windows 10은 항상 켜기라는 구성을 통해 기본적으로 이를 지원합니다.상시 작동에 대한 자세한 내용은 https://learn.microsoft.com/en-us/windows/access-protection/vpn/vpn-auto-trigger-profile 에서 확인할 수 있습니다.

MDM을 통해 배포하거나 WMI/Powershell을 사용하여 배포할 수 있습니다.

배포에 대한 참조

VPN 2 CSP: https://learn.microsoft.com/en-us/windows/client-management/mdm/vpnv2-csp

CSP에서 WMI 브리지로: https://learn.microsoft.com/en-us/windows/client-management/mdm/using-powershell-scripting-with-the-wmi-bridge-provider

Windows VPN 설정의 "자동으로 연결" 확인란이 잘 작동했습니다.그러나 VPN IP 주소로 잠긴 VM에 연결하도록 분할 터널링을 구성한 후 VPN 연결을 적용하려면 VPN 연결을 끊거나 다시 연결해야 했습니다.문제는rasdial /disconnect자동 트리거 설정을 비활성화합니다.아래 내용은 자동 트리거링을 다시 활성화하는 데 도움이 되는 것 같습니다.

여기에서 특정 VPN 프로파일 이름을 설정하거나 Get-VpnConnection에서 반환되는 첫 번째 VPN 프로파일 이름을 사용합니다.

$vpnProfileName = Get-VpnConnection | select -first 1 -ExpandProperty Name

분할 터널링을 설정하는 방법을 보여주는 선택적 예:

# Enable split-tunneling to a specific address
# Name of VM restricted to VPN IP addresses
$vmName = "myserver.eastus.cloudapp.azure.com"
$ip = $(Resolve-DnsName -name $vmName  | where section -eq answer).IPAddress
Add-VpnConnectionRoute -Name $vpnProfileName -DestinationPrefix "$ip/32"

# Rasdial disconnect will turn off AutoTriggering
rasdial $vpnProfileName /disconnect

# Check VPN status
Get-VpnConnection | select Name, IsAutoTriggerEnabled, ConnectionStatus

자동 트리거링을 다시 활성화하고 VPN 연결을 시작합니다.

# Remove Disabled Profile
$disabledProfiles = [string[]](Get-ItemPropertyValue HKLM:SYSTEM\CurrentControlSet\Services\RasMan\Config -name AutoTriggerDisabledProfilesList)
$disabledProfiles = $disabledProfiles | where { $_ -ne $vpnProfileName }
Set-ItemProperty HKLM:SYSTEM\CurrentControlSet\Services\RasMan\Config -name AutoTriggerDisabledProfilesList -Type MultiString -Value $disabledProfiles

# Remove AutoTriggeringDisabled
Remove-ItemProperty HKLM:SYSTEM\CurrentControlSet\Services\RasMan\Config -name AutoTriggeringDisabled 

# Add trigger to a process that is certain to be running. Will trigger on process launch as well as if it is already running.
# Adding trigger even it already exists seems to be necessary to get it to trigger after rasdial /disconnect
Add-VpnConnectionTriggerApplication -Name $vpnProfileName –ApplicationID "C:\Windows\explorer.exe" -ErrorAction Ignore 

# Check VPN status
Get-VpnConnection | select Name, IsAutoTriggerEnabled, ConnectionStatus

언급URL : https://stackoverflow.com/questions/10619285/connect-to-vpn-by-powershell

반응형