Skip to content

Instantly share code, notes, and snippets.

@Shterneregen
Last active June 13, 2024 12:12
Show Gist options
  • Save Shterneregen/a512496243d2d996a50f63f6b2b6de7d to your computer and use it in GitHub Desktop.
Save Shterneregen/a512496243d2d996a50f63f6b2b6de7d to your computer and use it in GitHub Desktop.

Revisions

  1. Shterneregen revised this gist Jun 28, 2023. 2 changed files with 19 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions CreateScheduledTask.ps1
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    # Create CreateScheduledTask.ps1 file with content below.

    # And run as admin command in PowerShell:
    # - Create CreateScheduledTask.ps1 file with content below.
    # - Run as admin command in PowerShell:
    # Get-Content .\CreateScheduledTask.ps1 | PowerShell.exe -noprofile -
    # - Restart Windows
    # - Launch WSL
    # - Launch VPN
    # - Done! Metric automatically updated

    $taskname="Fix VPN for WSL"
    $scriptName = "UpdateAnyConnectInterfaceMetric.ps1"
    13 changes: 13 additions & 0 deletions wsl-on-logon.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Task to start WSL on Windows logon
    # Run as admin command in PowerShell:
    # Get-Content .\wsl-on-logon.ps1 | PowerShell.exe -noprofile -

    $taskname="Start WSL on logon"
    $trigger= New-ScheduledTaskTrigger -AtLogon
    $triggers = @()
    $triggers += $trigger
    $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 0
    $user = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName
    $action = New-ScheduledTaskAction -Execute "wsl"

    Register-ScheduledTask -TaskName $taskname -Trigger $triggers -User $user -Action $action -Settings $settings -RunLevel Highest -Force
  2. Shterneregen revised this gist Jun 4, 2023. No changes.
  3. Shterneregen revised this gist Jun 4, 2023. 1 changed file with 16 additions and 13 deletions.
    29 changes: 16 additions & 13 deletions CreateScheduledTask.ps1
    Original file line number Diff line number Diff line change
    @@ -3,30 +3,33 @@
    # And run as admin command in PowerShell:
    # Get-Content .\CreateScheduledTask.ps1 | PowerShell.exe -noprofile -

    $profilePath = $env:USERPROFILE
    $taskname="Fix VPN for WSL"
    $scriptName = "UpdateAnyConnectInterfaceMetric.ps1"
    $commandToUpdateMetric = "Get-NetAdapter | Where-Object {`$_.InterfaceDescription -Match 'Cisco AnyConnect'} | Set-NetIPInterface -InterfaceMetric 5500"

    $profilePath = $env:USERPROFILE
    $scriptPath = "$profilePath\$scriptName"

    if(!(Test-Path -Path $scriptPath)){
    New-Item -ItemType File -Path $scriptPath -Value "Get-NetAdapter | Where-Object {`$_.InterfaceDescription -Match 'Cisco AnyConnect'} | Set-NetIPInterface -InterfaceMetric 5500"
    Write-Output "File '$scriptPath' created successfully"
    } else {
    if(Test-Path -Path $scriptPath){
    Write-Output "File '$scriptPath' already exists"
    } else {
    New-Item -ItemType File -Path $scriptPath -Value $commandToUpdateMetric
    Write-Output "File '$scriptPath' created successfully"
    }

    $taskname="Fix VPN for WSL"
    $triggers = @()

    $CIMTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
    $trigger = New-CimInstance -CimClass $CIMTriggerClass -ClientOnly
    $triggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
    $trigger = New-CimInstance -CimClass $triggerClass -ClientOnly
    $trigger.Subscription =
    @"
    <QueryList><Query Id="0" Path="Cisco AnyConnect Secure Mobility Client"><Select Path="Cisco AnyConnect Secure Mobility Client">*[System[Provider[@Name='acvpnagent'] and EventID=2039]]</Select></Query></QueryList>
    "@
    $trigger.Enabled = $True

    $triggers = @()
    $triggers += $trigger
    $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 0

    $User = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName
    $Action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Command Get-Content '$scriptPath' | PowerShell.exe -noprofile -"
    $user = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName
    $action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Command Get-Content '$scriptPath' | PowerShell.exe -noprofile -"

    Register-ScheduledTask -TaskName $taskname -Trigger $triggers -User $User -Action $Action -RunLevel Highest -Force
    Register-ScheduledTask -TaskName $taskname -Trigger $triggers -User $user -Action $action -Settings $settings -RunLevel Highest -Force
  4. Shterneregen revised this gist Jun 1, 2023. No changes.
  5. Shterneregen created this gist Jun 1, 2023.
    32 changes: 32 additions & 0 deletions CreateScheduledTask.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # Create CreateScheduledTask.ps1 file with content below.

    # And run as admin command in PowerShell:
    # Get-Content .\CreateScheduledTask.ps1 | PowerShell.exe -noprofile -

    $profilePath = $env:USERPROFILE
    $scriptName = "UpdateAnyConnectInterfaceMetric.ps1"
    $scriptPath = "$profilePath\$scriptName"

    if(!(Test-Path -Path $scriptPath)){
    New-Item -ItemType File -Path $scriptPath -Value "Get-NetAdapter | Where-Object {`$_.InterfaceDescription -Match 'Cisco AnyConnect'} | Set-NetIPInterface -InterfaceMetric 5500"
    Write-Output "File '$scriptPath' created successfully"
    } else {
    Write-Output "File '$scriptPath' already exists"
    }

    $taskname="Fix VPN for WSL"
    $triggers = @()

    $CIMTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
    $trigger = New-CimInstance -CimClass $CIMTriggerClass -ClientOnly
    $trigger.Subscription =
    @"
    <QueryList><Query Id="0" Path="Cisco AnyConnect Secure Mobility Client"><Select Path="Cisco AnyConnect Secure Mobility Client">*[System[Provider[@Name='acvpnagent'] and EventID=2039]]</Select></Query></QueryList>
    "@
    $trigger.Enabled = $True
    $triggers += $trigger

    $User = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName
    $Action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Command Get-Content '$scriptPath' | PowerShell.exe -noprofile -"

    Register-ScheduledTask -TaskName $taskname -Trigger $triggers -User $User -Action $Action -RunLevel Highest -Force