PowerShell – Task Scheduler and Troubleshooting with Network Location Awareness Service

You come to work and it turns out you can’t connect with business applications and services. Most likely, this problem is caused by a slow connection.

Script for adding a task to scheduler is illustrated by the example of the Network Location Awareness Service restart 1 minute after the computer start. This solution solves temporarily (I hope) the problem with an incorrect recognition of the network by the system after the replacement of network devices.

#-------------[deklarations]--------------------------
$taskName="RestartNetworkLocationAwernessService"
$task=Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
$taskAction=New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Service -Force -Name NlaSvc'
$taskTrigger=New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:01:00
$taskSettings=New-ScheduledTaskSettingsSet -Compatibility Win8
$taskPrincipal=New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest
$taskDefinition=New-ScheduledTask -Action $taskAction -Trigger $taskTrigger -Settings $taskSettings -Principal $taskPrincipal
#-------------[functions]--------------------------
function createTask {
    Write-host "Creating the new task" -foreground 'gray' 
    Register-ScheduledTask -TaskName $taskName -InputObject $taskDefinition
    Write-host "The task created successfully" -foreground 'green'
}

createTask
#-------------krolaki.eu--------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *