QuânSysAd's Blog: window10
Hiển thị các bài đăng có nhãn window10. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn window10. Hiển thị tất cả bài đăng

13 tháng 11 2025

Hotspot Window 10 thỉnh thoảng đang chạy một thời gian không kết nối lại được.

 Tốt nhất là tạo script Powershell sau và thực hiện đặt schedule task để chạy lại sau một số khoảng thời gian. 


# Yêu cầu quyền Administrator

# if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

# {

#     Write-Warning "Script này cần quyền Administrator. Vui lòng chạy PowerShell với quyền Administrator!"

#     Break

# }


Add-Type -AssemblyName System.Runtime.WindowsRuntime


# Tìm phương thức AsTask cho Generic

$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {

    $_.Name -eq 'AsTask' -and

    $_.GetParameters().Count -eq 1 -and

    $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'

})[0]


# Function đợi kết quả từ Windows Runtime Async Operation

Function Await($WinRtTask, $ResultType) {

    try {

        $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)

        $netTask = $asTask.Invoke($null, @($WinRtTask))


        # Đợi task hoàn thành

        $completed = $netTask.Wait(30000) # Timeout 30 giây


        if (-not $completed) {

            throw "Operation timed out after 30 seconds"

        }


        # Kiểm tra lỗi

        if ($netTask.IsFaulted) {

            throw $netTask.Exception.InnerException

        }


        return $netTask.Result

    }

    catch {

        Write-Error "Await error: $($_.Exception.Message)"

        throw

    }

}


# Function đợi hoàn thành Windows Runtime Async Action

Function AwaitAction($WinRtAction) {

    try {

        $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {

            $_.Name -eq 'AsTask' -and

            $_.GetParameters().Count -eq 1 -and

            !$_.IsGenericMethod

        })[0]

        $netTask = $asTask.Invoke($null, @($WinRtAction))


        # Đợi task hoàn thành

        $completed = $netTask.Wait(30000) # Timeout 30 giây


        if (-not $completed) {

            throw "Operation timed out after 30 seconds"

        }


        # Kiểm tra lỗi

        if ($netTask.IsFaulted) {

            throw $netTask.Exception.InnerException

        }

    }

    catch {

        Write-Error "AwaitAction error: $($_.Exception.Message)"

        throw

    }

}


try {

    # Load Windows Runtime assemblies

    [Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType = WindowsRuntime] | Out-Null

    [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType = WindowsRuntime] | Out-Null


    # Lấy connection profile

    $connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()


    if ($null -eq $connectionProfile) {

        Write-Error "Không tìm thấy kết nối Internet. Vui lòng kiểm tra kết nối mạng của bạn."

        Break

    }


    # Tạo tethering manager

    $tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager]::CreateFromConnectionProfile($connectionProfile)


    # Kiểm tra khả năng tethering

    # if ($tetheringManager.TetheringCapability -ne 0) {

    #     Write-Warning "Hotspot không khả dụng trên thiết bị này. Lý do: $($tetheringManager.TetheringCapability)"

    #     Break

    # }


    # Kiểm tra trạng thái hiện tại

    if ($tetheringManager.TetheringOperationalState -eq 1) {

        Write-Host "Hotspot đang bat! Đang khoi dong lai..." -ForegroundColor Yellow


        # Tắt hotspot

        $stopResult = Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])


        if ($stopResult.Status -eq 0) {

            Write-Host "Da bat lai hotspot thanh cong." -ForegroundColor Green

            Start-Sleep -Seconds 2


            # Bật lại hotspot

            $startResult = Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])


            if ($startResult.Status -eq 0) {

                Write-Host "Da bat lai hotspot thanh cong !" -ForegroundColor Green

            } else {

                Write-Error "Không thể bật lại hotspot. Mã lỗi: $($startResult.Status)"

            }

        } else {

            Write-Error "Không thể tắt hotspot. Mã lỗi: $($stopResult.Status)"

        }

    }

    else {

        Write-Host "Hotspot đang tat! Đang bat len..." -ForegroundColor Yellow


        # Bật hotspot

        $startResult = Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])


        if ($startResult.Status -eq 0) {

            Write-Host "Da bat hotspot thanh cong!" -ForegroundColor Green

        } else {

            Write-Error "Không thể bật hotspot. Mã lỗi: $($startResult.Status)"

        }

    }

}

catch {

    Write-Error "Đã xảy ra lỗi: $($_.Exception.Message)"

    Write-Host "`nGợi ý khắc phục:" -ForegroundColor Cyan

    Write-Host "1. Chạy PowerShell với quyền Administrator" -ForegroundColor White

    Write-Host "2. Kiểm tra xem thiết bị có hỗ trợ Mobile Hotspot không" -ForegroundColor White

    Write-Host "3. Đảm bảo có kết nối mạng hoạt động (Ethernet hoặc Wi-Fi)" -ForegroundColor White

    Write-Host "4. Kiểm tra Windows Update và driver mạng" -ForegroundColor White

}