Issue with wg-easy VPN service and setting up auto reboot using Powershell script on Automation Accounts

52 Views Asked by At

I am experiencing issues with the wg-easy VPN service, as the VPS containers seem to go down frequently. I have set up a cron job to reboot every 2 hours, but it doesn't seem to help much.

I am also interested in setting up an auto reboot using a Powershell script on Automation Accounts, but I am not sure if my runbook script is correct. Here is the script I have written:

$resourceGroupName = "xxx-resource-group"
$vmNames = @(
    "xxx-virtual-machine",
    "xxx_virtual_machine",
    "xxx-virtual-machine"
)

$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)

Register-ScheduledJob -ScriptBlock {
    Connect-AzAccount -Identity

    foreach ($vmName in $vmNames) {
        Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
    }
} -Trigger $trigger

I would appreciate any help with troubleshooting the wg-easy VPN service issues and with verifying if my Powershell script is correct.

Thank you.

enter image description here

1

There are 1 best solutions below

3
Jahnavi On

Setting up an auto reboot using a PowerShell script on Automation Accounts, but I am not sure if my runbook script is correct:

Your script looks good to me and below are the improvised versions of the script to make it work efficiently.

$resourceGroupName = "xxx"
$vmName = "latestj"

$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)

Register-ScheduledJob -ScriptBlock {
    param (
        [string]$resourceGroupName,
        [string[]]$vmName
    )

    Connect-AzAccount -Identity
    Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
} -Trigger $trigger -ArgumentList $resourceGroupName, $vmName

You can also use try catch blocks to check and handle errors.

enter image description here

$resourceGroupName = "xxx"
$vmNames = @(
    xxxx
)
$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)
Register-ScheduledJob -ScriptBlock {
    Connect-AzAccount -Identity

    foreach ($vmName in $vmNames) {
        $vmdetails = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -ErrorAction SilentlyContinue
        if ($vmdetails) {
            $Status = $vmdetails.ProvisioningState
            if ($Status -eq "Succeeded") {
                Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
                Write-Host "Restarted"
            } else {
                Write-Host "Not in a running state"
            }
        } else {
            Write-Host "VM $vmName not found"
        }
    }
} -Trigger $trigger

Coming to the,

Issue with wg-easy VPN service:

Although using a cron job to reboot every 2 hours makes sure the service stays online, it's not the most feasible approach. It interrupts connections and poses the risk of data loss.

  • Check wg-easy VPN network configurations and also verify you are using the latest version of it.
  • Also check wg-easy logs and monitor the VPN usage that might cause these conflicts.