连接VPN后自动连接网络驱动器

问题描述 投票:0回答:1

我有几个映射的网络驱动器,只能在公司 LAN 上或使用 OpenVPN 连接到 VPN 时访问。

当我连接到办公室的网络时,驱动器会自动连接。但是,当我通过 VPN 连接时,只有两个驱动器自动连接(我尝试过

net use /persistent:yes
但这似乎没有什么区别,所以也许我不理解该命令)。

令我感到奇怪的是,当我输入

net use
时,我的其余驱动器状态显示为“不可用”。如果我在 Windows 资源管理器中单击该驱动器,驱动器状态将为空白。当我使用
get-PSDrive
时,这些驱动器不会显示。不确定这是否与自动连接有关。

关于如何自动连接到其他驱动器,有什么想法吗?

powershell vpn openvpn network-drive
1个回答
0
投票

这是最终对我有用的解决方案:

我使用任务计划程序创建了一个任务,该任务在我访问 OpenVPN 时自动运行。这些任务使用以下代码启动 .cmd 文件:

@ECHO OFF
PowerShell -Command "Set-ExecutionPolicy -Scope CurrentUser Unrestricted" >> "C:\Users\USERNAME\AppData\Local\Temp\StartupLog.txt" 2>&1
PowerShell -File "C:\Scripts\MapDrives.ps1" 

这将调用以下 PowerShell 脚本。

$start = Get-Date
$endTime = $start.AddMinutes(3)  # Set end time 3 minutes from start

$MappedDrives = Get-SmbMapping | Where-Object { $_.Status -ne "Ok" } | Select-Object LocalPath, RemotePath, Status

# Function to log messages to a file
function LogMessage {
    param(
        [string]$Message,
        [string]$Color,
        [switch]$Display=$False
    )

    $logFilePath = Join-Path -Path $scriptDirectory -ChildPath "mappinglog.txt"
    Add-Content -Path $logFilePath -Value $Message
    if( $Display ) {
        Write-Host $Message -ForegroundColor $Color
    }
}

# Create or append to the log file
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$logFilePath = Join-Path -Path $scriptDirectory -ChildPath "mappinglog.txt"
if (Test-Path $logFilePath) {
    Remove-Item -Path $logFilePath 
}

New-Item -Path $logFilePath -ItemType File | Out-Null

Write-Host "Reconnecting drives...." -ForegroundColor Cyan
Write-Host "Press the CTRL+C hot key combination to cancel." -ForegroundColor Cyan

while ($MappedDrives -and (Get-Date) -lt $endTime) {
    $error.clear()
    foreach ($MappedDrive in $MappedDrives) {
        if ($MappedDrive.Status -eq "Disconnected") {
            if (Test-Path $MappedDrive.LocalPath) {
                LogMessage "Success: $($MappedDrive.LocalPath)\ reconnected to $($MappedDrive.RemotePath)" -Color Green -Display $True
            } else {
                LogMessage "Failed: Unable to reconnect $($MappedDrive.LocalPath)\ to $($MappedDrive.RemotePath)" -Color Red
            }
        } elseif ($MappedDrive.Status -eq "Unavailable") {
            if (Test-Path $MappedDrive.RemotePath) {
                New-SmbMapping -LocalPath $MappedDrive.LocalPath -RemotePath $MappedDrive.RemotePath -Persistent $true
            }

            if (Test-Path $MappedDrive.LocalPath) {
                LogMessage "Success: $($MappedDrive.LocalPath)\ connected to $($MappedDrive.RemotePath)" -Color Green -Display $True
            } else {
                LogMessage "Failed: Unable to connect $($MappedDrive.LocalPath)\ to $($MappedDrive.RemotePath)" -Color Red
            }
        } elseif ($MappedDrive.Status -eq "Reconnecting") {
            LogMessage "Pending: $($MappedDrive.LocalPath)\ is automatically reconnecting to $($MappedDrive.RemotePath)" -Color White -Display $True
        }
    }
    $MappedDrives = Get-SmbMapping | Where-Object { $_.Status -ne "Ok" } | Select-Object LocalPath, RemotePath, Status
}


© www.soinside.com 2019 - 2024. All rights reserved.